aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/kobject.txt489
-rw-r--r--Documentation/pnp.txt4
-rw-r--r--Documentation/s390/cds.txt2
-rw-r--r--Documentation/vm/slabinfo.c2
-rw-r--r--Documentation/vm/slub.txt2
-rw-r--r--Documentation/zh_CN/CodingStyle701
-rw-r--r--Documentation/zh_CN/HOWTO10
-rw-r--r--Documentation/zh_CN/SubmittingDrivers168
-rw-r--r--Documentation/zh_CN/SubmittingPatches416
-rw-r--r--Documentation/zh_CN/oops-tracing.txt212
-rw-r--r--Documentation/zh_CN/sparse.txt100
-rw-r--r--Documentation/zh_CN/stable_kernel_rules.txt66
-rw-r--r--Documentation/zh_CN/volatile-considered-harmful.txt113
-rw-r--r--arch/arm/kernel/time.c4
-rw-r--r--arch/arm/mach-integrator/integrator_ap.c2
-rw-r--r--arch/arm/mach-omap1/pm.c23
-rw-r--r--arch/arm/mach-pxa/cm-x270.c2
-rw-r--r--arch/arm/mach-pxa/lpd270.c2
-rw-r--r--arch/arm/mach-pxa/lubbock.c2
-rw-r--r--arch/arm/mach-pxa/mainstone.c2
-rw-r--r--arch/arm/mach-s3c2410/s3c2410.c2
-rw-r--r--arch/arm/mach-s3c2412/s3c2412.c2
-rw-r--r--arch/arm/mach-s3c2440/mach-osiris.c2
-rw-r--r--arch/arm/mach-s3c2443/s3c2443.c2
-rw-r--r--arch/arm/mach-sa1100/irq.c2
-rw-r--r--arch/arm/oprofile/common.c2
-rw-r--r--arch/arm/plat-omap/gpio.c2
-rw-r--r--arch/arm/plat-s3c24xx/dma.c2
-rw-r--r--arch/arm/plat-s3c24xx/s3c244x.c4
-rw-r--r--arch/avr32/kernel/time.c2
-rw-r--r--arch/cris/arch-v32/drivers/iop_fw_load.c11
-rw-r--r--arch/ia64/kernel/topology.c26
-rw-r--r--arch/mips/kernel/i8259.c2
-rw-r--r--arch/powerpc/platforms/cell/spu_base.c2
-rw-r--r--arch/powerpc/platforms/powermac/pic.c2
-rw-r--r--arch/powerpc/platforms/pseries/power.c32
-rw-r--r--arch/powerpc/sysdev/ipic.c2
-rw-r--r--arch/powerpc/sysdev/mpic.c2
-rw-r--r--arch/powerpc/sysdev/qe_lib/qe_ic.c2
-rw-r--r--arch/ppc/syslib/ipic.c2
-rw-r--r--arch/ppc/syslib/open_pic.c2
-rw-r--r--arch/ppc/syslib/open_pic2.c2
-rw-r--r--arch/s390/hypfs/inode.c13
-rw-r--r--arch/s390/kernel/ipl.c147
-rw-r--r--arch/s390/kernel/time.c2
-rw-r--r--arch/sh/drivers/dma/dma-sysfs.c2
-rw-r--r--arch/sh/kernel/cpu/sh4/sq.c13
-rw-r--r--arch/sh/kernel/time.c2
-rw-r--r--arch/x86/kernel/apic_32.c2
-rw-r--r--arch/x86/kernel/apic_64.c2
-rw-r--r--arch/x86/kernel/cpu/intel_cacheinfo.c24
-rw-r--r--arch/x86/kernel/cpu/mcheck/mce_64.c2
-rw-r--r--arch/x86/kernel/cpu/mcheck/mce_amd_64.c30
-rw-r--r--arch/x86/kernel/cpuid.c6
-rw-r--r--arch/x86/kernel/i8237.c2
-rw-r--r--arch/x86/kernel/i8259_32.c2
-rw-r--r--arch/x86/kernel/i8259_64.c2
-rw-r--r--arch/x86/kernel/io_apic_32.c2
-rw-r--r--arch/x86/kernel/io_apic_64.c2
-rw-r--r--arch/x86/kernel/msr.c6
-rw-r--r--arch/x86/kernel/nmi_32.c2
-rw-r--r--arch/x86/kernel/nmi_64.c2
-rw-r--r--arch/x86/oprofile/nmi_int.c2
-rw-r--r--block/elevator.c8
-rw-r--r--block/genhd.c419
-rw-r--r--block/ll_rw_blk.c11
-rw-r--r--drivers/acpi/bus.c11
-rw-r--r--drivers/acpi/pci_link.c2
-rw-r--r--drivers/acpi/system.c13
-rw-r--r--drivers/base/Makefile3
-rw-r--r--drivers/base/attribute_container.c9
-rw-r--r--drivers/base/base.h64
-rw-r--r--drivers/base/bus.c501
-rw-r--r--drivers/base/class.c319
-rw-r--r--drivers/base/core.c582
-rw-r--r--drivers/base/cpu.c2
-rw-r--r--drivers/base/dd.c164
-rw-r--r--drivers/base/driver.c216
-rw-r--r--drivers/base/firmware.c26
-rw-r--r--drivers/base/hypervisor.c12
-rw-r--r--drivers/base/init.c10
-rw-r--r--drivers/base/memory.c2
-rw-r--r--drivers/base/module.c94
-rw-r--r--drivers/base/node.c2
-rw-r--r--drivers/base/platform.c239
-rw-r--r--drivers/base/power/Makefile1
-rw-r--r--drivers/base/power/main.c502
-rw-r--r--drivers/base/power/power.h19
-rw-r--r--drivers/base/power/shutdown.c48
-rw-r--r--drivers/base/sys.c49
-rw-r--r--drivers/block/aoe/aoeblk.c51
-rw-r--r--drivers/block/aoe/aoechr.c7
-rw-r--r--drivers/block/nbd.c15
-rw-r--r--drivers/block/paride/pg.c6
-rw-r--r--drivers/block/paride/pt.c12
-rw-r--r--drivers/block/pktcdvd.c27
-rw-r--r--drivers/char/Kconfig10
-rw-r--r--drivers/char/Makefile1
-rw-r--r--drivers/char/hvc_console.c80
-rw-r--r--drivers/char/hvcs.c78
-rw-r--r--drivers/char/nozomi.c1993
-rw-r--r--drivers/cpufreq/cpufreq.c12
-rw-r--r--drivers/cpuidle/sysfs.c21
-rw-r--r--drivers/dma/dmaengine.c43
-rw-r--r--drivers/edac/edac_device_sysfs.c49
-rw-r--r--drivers/edac/edac_mc_sysfs.c40
-rw-r--r--drivers/edac/edac_module.c2
-rw-r--r--drivers/edac/edac_pci_sysfs.c35
-rw-r--r--drivers/firmware/edd.c25
-rw-r--r--drivers/firmware/efivars.c130
-rw-r--r--drivers/i2c/chips/isp1301_omap.c6
-rw-r--r--drivers/ide/ide-probe.c2
-rw-r--r--drivers/ide/ide-tape.c14
-rw-r--r--drivers/ieee1394/nodemgr.c314
-rw-r--r--drivers/infiniband/core/sysfs.c39
-rw-r--r--drivers/infiniband/hw/ehca/ehca_main.c13
-rw-r--r--drivers/infiniband/hw/ipath/ipath_driver.c17
-rw-r--r--drivers/infiniband/hw/ipath/ipath_kernel.h3
-rw-r--r--drivers/infiniband/hw/ipath/ipath_sysfs.c30
-rw-r--r--drivers/isdn/capi/capi.c6
-rw-r--r--drivers/isdn/gigaset/gigaset.h6
-rw-r--r--drivers/kvm/kvm_main.c2
-rw-r--r--drivers/macintosh/adb.c2
-rw-r--r--drivers/macintosh/via-pmu.c2
-rw-r--r--drivers/md/dm.c4
-rw-r--r--drivers/md/md.c31
-rw-r--r--drivers/mfd/ucb1x00-assabet.c17
-rw-r--r--drivers/mfd/ucb1x00-core.c14
-rw-r--r--drivers/mfd/ucb1x00.h4
-rw-r--r--drivers/misc/ibmasm/command.c12
-rw-r--r--drivers/misc/ibmasm/ibmasm.h10
-rw-r--r--drivers/misc/tifm_7xx1.c4
-rw-r--r--drivers/misc/tifm_core.c24
-rw-r--r--drivers/mtd/mtdchar.c12
-rw-r--r--drivers/net/ibmveth.c12
-rw-r--r--drivers/net/iseries_veth.c19
-rw-r--r--drivers/net/wan/cosa.c5
-rw-r--r--drivers/parisc/pdc_stable.c194
-rw-r--r--drivers/pci/hotplug/acpiphp_ibm.c4
-rw-r--r--drivers/pci/hotplug/pci_hotplug_core.c39
-rw-r--r--drivers/pci/hotplug/rpadlpar_sysfs.c113
-rw-r--r--drivers/pci/pci-driver.c59
-rw-r--r--drivers/pci/probe.c11
-rw-r--r--drivers/pcmcia/ds.c3
-rw-r--r--drivers/power/apm_power.c116
-rw-r--r--drivers/power/power_supply_core.c74
-rw-r--r--drivers/rtc/interface.c22
-rw-r--r--drivers/s390/net/netiucv.c16
-rw-r--r--drivers/s390/scsi/zfcp_ccw.c14
-rw-r--r--drivers/s390/scsi/zfcp_ext.h3
-rw-r--r--drivers/s390/scsi/zfcp_sysfs_driver.c27
-rw-r--r--drivers/scsi/hosts.c24
-rw-r--r--drivers/scsi/libsas/sas_scsi_host.c2
-rw-r--r--drivers/serial/icom.c24
-rw-r--r--drivers/serial/icom.h2
-rw-r--r--drivers/spi/spi.c26
-rw-r--r--drivers/uio/uio.c95
-rw-r--r--drivers/usb/core/driver.c8
-rw-r--r--fs/block_dev.c8
-rw-r--r--fs/char_dev.c6
-rw-r--r--fs/coda/psdev.c8
-rw-r--r--fs/configfs/mount.c13
-rw-r--r--fs/debugfs/inode.c13
-rw-r--r--fs/dlm/lockspace.c50
-rw-r--r--fs/ecryptfs/main.c129
-rw-r--r--fs/fuse/inode.c26
-rw-r--r--fs/gfs2/locking/dlm/sysfs.c38
-rw-r--r--fs/gfs2/sys.c29
-rw-r--r--fs/namespace.c11
-rw-r--r--fs/ocfs2/cluster/masklog.c4
-rw-r--r--fs/ocfs2/cluster/sys.c83
-rw-r--r--fs/partitions/check.c327
-rw-r--r--fs/sysfs/dir.c2
-rw-r--r--fs/sysfs/file.c67
-rw-r--r--fs/sysfs/symlink.c88
-rw-r--r--include/acpi/acpi_bus.h2
-rw-r--r--include/linux/device.h360
-rw-r--r--include/linux/dmaengine.h3
-rw-r--r--include/linux/fs.h2
-rw-r--r--include/linux/genhd.h37
-rw-r--r--include/linux/kobject.h184
-rw-r--r--include/linux/kref.h1
-rw-r--r--include/linux/module.h19
-rw-r--r--include/linux/pci_hotplug.h2
-rw-r--r--include/linux/pktcdvd.h2
-rw-r--r--include/linux/platform_device.h2
-rw-r--r--include/linux/sched.h9
-rw-r--r--include/linux/sysdev.h1
-rw-r--r--include/linux/tifm.h2
-rw-r--r--include/linux/uio_driver.h6
-rw-r--r--include/rdma/ib_verbs.h2
-rw-r--r--init/Kconfig1
-rw-r--r--init/do_mounts.c108
-rw-r--r--kernel/ksysfs.c82
-rw-r--r--kernel/module.c128
-rw-r--r--kernel/params.c44
-rw-r--r--kernel/power/disk.c20
-rw-r--r--kernel/power/main.c23
-rw-r--r--kernel/power/power.h4
-rw-r--r--kernel/rtmutex-tester.c2
-rw-r--r--kernel/time/clocksource.c2
-rw-r--r--kernel/time/timekeeping.c2
-rw-r--r--kernel/user.c107
-rw-r--r--lib/kobject.c734
-rw-r--r--lib/kobject_uevent.c38
-rw-r--r--lib/kref.c15
-rw-r--r--mm/slub.c23
-rw-r--r--net/bridge/br_if.c12
-rw-r--r--net/bridge/br_private.h2
-rw-r--r--net/bridge/br_sysfs_br.c14
-rw-r--r--net/bridge/br_sysfs_if.c2
-rw-r--r--samples/Kconfig10
-rw-r--r--samples/Makefile2
-rw-r--r--samples/kobject/Makefile1
-rw-r--r--samples/kobject/kobject-example.c137
-rw-r--r--samples/kobject/kset-example.c278
-rw-r--r--security/inode.c11
217 files changed, 8870 insertions, 4631 deletions
diff --git a/Documentation/kobject.txt b/Documentation/kobject.txt
index ca86a885ad8f..bf3256e04027 100644
--- a/Documentation/kobject.txt
+++ b/Documentation/kobject.txt
@@ -1,289 +1,386 @@
1The kobject Infrastructure 1Everything you never wanted to know about kobjects, ksets, and ktypes
2 2
3Patrick Mochel <mochel@osdl.org> 3Greg Kroah-Hartman <gregkh@suse.de>
4 4
5Updated: 3 June 2003 5Based on an original article by Jon Corbet for lwn.net written October 1,
62003 and located at http://lwn.net/Articles/51437/
6 7
8Last updated December 19, 2007
7 9
8Copyright (c) 2003 Patrick Mochel
9Copyright (c) 2003 Open Source Development Labs
10 10
11Part of the difficulty in understanding the driver model - and the kobject
12abstraction upon which it is built - is that there is no obvious starting
13place. Dealing with kobjects requires understanding a few different types,
14all of which make reference to each other. In an attempt to make things
15easier, we'll take a multi-pass approach, starting with vague terms and
16adding detail as we go. To that end, here are some quick definitions of
17some terms we will be working with.
11 18
120. Introduction 19 - A kobject is an object of type struct kobject. Kobjects have a name
20 and a reference count. A kobject also has a parent pointer (allowing
21 objects to be arranged into hierarchies), a specific type, and,
22 usually, a representation in the sysfs virtual filesystem.
13 23
14The kobject infrastructure performs basic object management that larger 24 Kobjects are generally not interesting on their own; instead, they are
15data structures and subsystems can leverage, rather than reimplement 25 usually embedded within some other structure which contains the stuff
16similar functionality. This functionality primarily concerns: 26 the code is really interested in.
17 27
18- Object reference counting. 28 No structure should EVER have more than one kobject embedded within it.
19- Maintaining lists (sets) of objects. 29 If it does, the reference counting for the object is sure to be messed
20- Object set locking. 30 up and incorrect, and your code will be buggy. So do not do this.
21- Userspace representation.
22 31
23The infrastructure consists of a number of object types to support 32 - A ktype is the type of object that embeds a kobject. Every structure
24this functionality. Their programming interfaces are described below 33 that embeds a kobject needs a corresponding ktype. The ktype controls
25in detail, and briefly here: 34 what happens to the kobject when it is created and destroyed.
26 35
27- kobjects a simple object. 36 - A kset is a group of kobjects. These kobjects can be of the same ktype
28- kset a set of objects of a certain type. 37 or belong to different ktypes. The kset is the basic container type for
29- ktype a set of helpers for objects of a common type. 38 collections of kobjects. Ksets contain their own kobjects, but you can
39 safely ignore that implementation detail as the kset core code handles
40 this kobject automatically.
30 41
42 When you see a sysfs directory full of other directories, generally each
43 of those directories corresponds to a kobject in the same kset.
31 44
32The kobject infrastructure maintains a close relationship with the 45We'll look at how to create and manipulate all of these types. A bottom-up
33sysfs filesystem. Each kobject that is registered with the kobject 46approach will be taken, so we'll go back to kobjects.
34core receives a directory in sysfs. Attributes about the kobject can
35then be exported. Please see Documentation/filesystems/sysfs.txt for
36more information.
37 47
38The kobject infrastructure provides a flexible programming interface,
39and allows kobjects and ksets to be used without being registered
40(i.e. with no sysfs representation). This is also described later.
41 48
49Embedding kobjects
42 50
431. kobjects 51It is rare for kernel code to create a standalone kobject, with one major
52exception explained below. Instead, kobjects are used to control access to
53a larger, domain-specific object. To this end, kobjects will be found
54embedded in other structures. If you are used to thinking of things in
55object-oriented terms, kobjects can be seen as a top-level, abstract class
56from which other classes are derived. A kobject implements a set of
57capabilities which are not particularly useful by themselves, but which are
58nice to have in other objects. The C language does not allow for the
59direct expression of inheritance, so other techniques - such as structure
60embedding - must be used.
44 61
451.1 Description 62So, for example, the UIO code has a structure that defines the memory
63region associated with a uio device:
46 64
65struct uio_mem {
66 struct kobject kobj;
67 unsigned long addr;
68 unsigned long size;
69 int memtype;
70 void __iomem *internal_addr;
71};
47 72
48struct kobject is a simple data type that provides a foundation for 73If you have a struct uio_mem structure, finding its embedded kobject is
49more complex object types. It provides a set of basic fields that 74just a matter of using the kobj member. Code that works with kobjects will
50almost all complex data types share. kobjects are intended to be 75often have the opposite problem, however: given a struct kobject pointer,
51embedded in larger data structures and replace fields they duplicate. 76what is the pointer to the containing structure? You must avoid tricks
77(such as assuming that the kobject is at the beginning of the structure)
78and, instead, use the container_of() macro, found in <linux/kernel.h>:
52 79
531.2 Definition 80 container_of(pointer, type, member)
54 81
55struct kobject { 82where pointer is the pointer to the embedded kobject, type is the type of
56 const char * k_name; 83the containing structure, and member is the name of the structure field to
57 struct kref kref; 84which pointer points. The return value from container_of() is a pointer to
58 struct list_head entry; 85the given type. So, for example, a pointer "kp" to a struct kobject
59 struct kobject * parent; 86embedded within a struct uio_mem could be converted to a pointer to the
60 struct kset * kset; 87containing uio_mem structure with:
61 struct kobj_type * ktype;
62 struct sysfs_dirent * sd;
63 wait_queue_head_t poll;
64};
65 88
66void kobject_init(struct kobject *); 89 struct uio_mem *u_mem = container_of(kp, struct uio_mem, kobj);
67int kobject_add(struct kobject *);
68int kobject_register(struct kobject *);
69 90
70void kobject_del(struct kobject *); 91Programmers often define a simple macro for "back-casting" kobject pointers
71void kobject_unregister(struct kobject *); 92to the containing type.
72 93
73struct kobject * kobject_get(struct kobject *);
74void kobject_put(struct kobject *);
75 94
95Initialization of kobjects
76 96
771.3 kobject Programming Interface 97Code which creates a kobject must, of course, initialize that object. Some
98of the internal fields are setup with a (mandatory) call to kobject_init():
78 99
79kobjects may be dynamically added and removed from the kobject core 100 void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
80using kobject_register() and kobject_unregister(). Registration
81includes inserting the kobject in the list of its dominant kset and
82creating a directory for it in sysfs.
83 101
84Alternatively, one may use a kobject without adding it to its kset's list 102The ktype is required for a kobject to be created properly, as every kobject
85or exporting it via sysfs, by simply calling kobject_init(). An 103must have an associated kobj_type. After calling kobject_init(), to
86initialized kobject may later be added to the object hierarchy by 104register the kobject with sysfs, the function kobject_add() must be called:
87calling kobject_add(). An initialized kobject may be used for
88reference counting.
89 105
90Note: calling kobject_init() then kobject_add() is functionally 106 int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...);
91equivalent to calling kobject_register().
92 107
93When a kobject is unregistered, it is removed from its kset's list, 108This sets up the parent of the kobject and the name for the kobject
94removed from the sysfs filesystem, and its reference count is decremented. 109properly. If the kobject is to be associated with a specific kset,
95List and sysfs removal happen in kobject_del(), and may be called 110kobj->kset must be assigned before calling kobject_add(). If a kset is
96manually. kobject_put() decrements the reference count, and may also 111associated with a kobject, then the parent for the kobject can be set to
97be called manually. 112NULL in the call to kobject_add() and then the kobject's parent will be the
113kset itself.
98 114
99A kobject's reference count may be incremented with kobject_get(), 115As the name of the kobject is set when it is added to the kernel, the name
100which returns a valid reference to a kobject; and decremented with 116of the kobject should never be manipulated directly. If you must change
101kobject_put(). An object's reference count may only be incremented if 117the name of the kobject, call kobject_rename():
102it is already positive.
103 118
104When a kobject's reference count reaches 0, the method struct 119 int kobject_rename(struct kobject *kobj, const char *new_name);
105kobj_type::release() (which the kobject's kset points to) is called.
106This allows any memory allocated for the object to be freed.
107 120
121There is a function called kobject_set_name() but that is legacy cruft and
122is being removed. If your code needs to call this function, it is
123incorrect and needs to be fixed.
108 124
109NOTE!!! 125To properly access the name of the kobject, use the function
126kobject_name():
110 127
111It is _imperative_ that you supply a destructor for dynamically 128 const char *kobject_name(const struct kobject * kobj);
112allocated kobjects to free them if you are using kobject reference
113counts. The reference count controls the lifetime of the object.
114If it goes to 0, then it is assumed that the object will
115be freed and cannot be used.
116 129
117More importantly, you must free the object there, and not immediately 130There is a helper function to both initialize and add the kobject to the
118after an unregister call. If someone else is referencing the object 131kernel at the same time, called supprisingly enough kobject_init_and_add():
119(e.g. through a sysfs file), they will obtain a reference to the
120object, assume it's valid and operate on it. If the object is
121unregistered and freed in the meantime, the operation will then
122reference freed memory and go boom.
123 132
124This can be prevented, in the simplest case, by defining a release 133 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
125method and freeing the object from there only. Note that this will not 134 struct kobject *parent, const char *fmt, ...);
126secure reference count/object management models that use a dual
127reference count or do other wacky things with the reference count
128(like the networking layer).
129 135
136The arguments are the same as the individual kobject_init() and
137kobject_add() functions described above.
130 138
1311.4 sysfs
132 139
133Each kobject receives a directory in sysfs. This directory is created 140Uevents
134under the kobject's parent directory.
135 141
136If a kobject does not have a parent when it is registered, its parent 142After a kobject has been registered with the kobject core, you need to
137becomes its dominant kset. 143announce to the world that it has been created. This can be done with a
144call to kobject_uevent():
138 145
139If a kobject does not have a parent nor a dominant kset, its directory 146 int kobject_uevent(struct kobject *kobj, enum kobject_action action);
140is created at the top-level of the sysfs partition.
141 147
148Use the KOBJ_ADD action for when the kobject is first added to the kernel.
149This should be done only after any attributes or children of the kobject
150have been initialized properly, as userspace will instantly start to look
151for them when this call happens.
142 152
153When the kobject is removed from the kernel (details on how to do that is
154below), the uevent for KOBJ_REMOVE will be automatically created by the
155kobject core, so the caller does not have to worry about doing that by
156hand.
143 157
1442. ksets
145 158
1462.1 Description 159Reference counts
147 160
148A kset is a set of kobjects that are embedded in the same type. 161One of the key functions of a kobject is to serve as a reference counter
162for the object in which it is embedded. As long as references to the object
163exist, the object (and the code which supports it) must continue to exist.
164The low-level functions for manipulating a kobject's reference counts are:
149 165
166 struct kobject *kobject_get(struct kobject *kobj);
167 void kobject_put(struct kobject *kobj);
150 168
151struct kset { 169A successful call to kobject_get() will increment the kobject's reference
152 struct kobj_type * ktype; 170counter and return the pointer to the kobject.
153 struct list_head list;
154 struct kobject kobj;
155 struct kset_uevent_ops * uevent_ops;
156};
157 171
172When a reference is released, the call to kobject_put() will decrement the
173reference count and, possibly, free the object. Note that kobject_init()
174sets the reference count to one, so the code which sets up the kobject will
175need to do a kobject_put() eventually to release that reference.
158 176
159void kset_init(struct kset * k); 177Because kobjects are dynamic, they must not be declared statically or on
160int kset_add(struct kset * k); 178the stack, but instead, always allocated dynamically. Future versions of
161int kset_register(struct kset * k); 179the kernel will contain a run-time check for kobjects that are created
162void kset_unregister(struct kset * k); 180statically and will warn the developer of this improper usage.
163 181
164struct kset * kset_get(struct kset * k); 182If all that you want to use a kobject for is to provide a reference counter
165void kset_put(struct kset * k); 183for your structure, please use the struct kref instead; a kobject would be
184overkill. For more information on how to use struct kref, please see the
185file Documentation/kref.txt in the Linux kernel source tree.
166 186
167struct kobject * kset_find_obj(struct kset *, char *);
168 187
188Creating "simple" kobjects
169 189
170The type that the kobjects are embedded in is described by the ktype 190Sometimes all that a developer wants is a way to create a simple directory
171pointer. 191in the sysfs hierarchy, and not have to mess with the whole complication of
192ksets, show and store functions, and other details. This is the one
193exception where a single kobject should be created. To create such an
194entry, use the function:
172 195
173A kset contains a kobject itself, meaning that it may be registered in 196 struct kobject *kobject_create_and_add(char *name, struct kobject *parent);
174the kobject hierarchy and exported via sysfs. More importantly, the
175kset may be embedded in a larger data type, and may be part of another
176kset (of that object type).
177 197
178For example, a block device is an object (struct gendisk) that is 198This function will create a kobject and place it in sysfs in the location
179contained in a set of block devices. It may also contain a set of 199underneath the specified parent kobject. To create simple attributes
180partitions (struct hd_struct) that have been found on the device. The 200associated with this kobject, use:
181following code snippet illustrates how to express this properly.
182 201
183 struct gendisk * disk; 202 int sysfs_create_file(struct kobject *kobj, struct attribute *attr);
184 ... 203or
185 disk->kset.kobj.kset = &block_kset; 204 int sysfs_create_group(struct kobject *kobj, struct attribute_group *grp);
186 disk->kset.ktype = &partition_ktype;
187 kset_register(&disk->kset);
188 205
189- The kset that the disk's embedded object belongs to is the 206Both types of attributes used here, with a kobject that has been created
190 block_kset, and is pointed to by disk->kset.kobj.kset. 207with the kobject_create_and_add(), can be of type kobj_attribute, so no
208special custom attribute is needed to be created.
191 209
192- The type of objects on the disk's _subordinate_ list are partitions, 210See the example module, samples/kobject/kobject-example.c for an
193 and is set in disk->kset.ktype. 211implementation of a simple kobject and attributes.
194 212
195- The kset is then registered, which handles initializing and adding
196 the embedded kobject to the hierarchy.
197 213
198 214
1992.2 kset Programming Interface 215ktypes and release methods
200 216
201All kset functions, except kset_find_obj(), eventually forward the 217One important thing still missing from the discussion is what happens to a
202calls to their embedded kobjects after performing kset-specific 218kobject when its reference count reaches zero. The code which created the
203operations. ksets offer a similar programming model to kobjects: they 219kobject generally does not know when that will happen; if it did, there
204may be used after they are initialized, without registering them in 220would be little point in using a kobject in the first place. Even
205the hierarchy. 221predictable object lifecycles become more complicated when sysfs is brought
222in as other portions of the kernel can get a reference on any kobject that
223is registered in the system.
206 224
207kset_find_obj() may be used to locate a kobject with a particular 225The end result is that a structure protected by a kobject cannot be freed
208name. The kobject, if found, is returned. 226before its reference count goes to zero. The reference count is not under
227the direct control of the code which created the kobject. So that code must
228be notified asynchronously whenever the last reference to one of its
229kobjects goes away.
209 230
210There are also some helper functions which names point to the formerly 231Once you registered your kobject via kobject_add(), you must never use
211existing "struct subsystem", whose functions have been taken over by 232kfree() to free it directly. The only safe way is to use kobject_put(). It
212ksets. 233is good practice to always use kobject_put() after kobject_init() to avoid
234errors creeping in.
213 235
236This notification is done through a kobject's release() method. Usually
237such a method has a form like:
214 238
215decl_subsys(name,type,uevent_ops) 239 void my_object_release(struct kobject *kobj)
240 {
241 struct my_object *mine = container_of(kobj, struct my_object, kobj);
216 242
217Declares a kset named '<name>_subsys' of type <type> with 243 /* Perform any additional cleanup on this object, then... */
218uevent_ops <uevent_ops>. For example, 244 kfree(mine);
245 }
219 246
220decl_subsys(devices, &ktype_device, &device_uevent_ops); 247One important point cannot be overstated: every kobject must have a
248release() method, and the kobject must persist (in a consistent state)
249until that method is called. If these constraints are not met, the code is
250flawed. Note that the kernel will warn you if you forget to provide a
251release() method. Do not try to get rid of this warning by providing an
252"empty" release function; you will be mocked mercilessly by the kobject
253maintainer if you attempt this.
221 254
222is equivalent to doing: 255Note, the name of the kobject is available in the release function, but it
256must NOT be changed within this callback. Otherwise there will be a memory
257leak in the kobject core, which makes people unhappy.
223 258
224struct kset devices_subsys = { 259Interestingly, the release() method is not stored in the kobject itself;
225 .ktype = &ktype_devices, 260instead, it is associated with the ktype. So let us introduce struct
226 .uevent_ops = &device_uevent_ops, 261kobj_type:
227}; 262
228kobject_set_name(&devices_subsys, name); 263 struct kobj_type {
264 void (*release)(struct kobject *);
265 struct sysfs_ops *sysfs_ops;
266 struct attribute **default_attrs;
267 };
229 268
230The objects that are registered with a subsystem that use the 269This structure is used to describe a particular type of kobject (or, more
231subsystem's default list must have their kset ptr set properly. These 270correctly, of containing object). Every kobject needs to have an associated
232objects may have embedded kobjects or ksets. The 271kobj_type structure; a pointer to that structure must be specified when you
233following helper makes setting the kset easier: 272call kobject_init() or kobject_init_and_add().
234 273
274The release field in struct kobj_type is, of course, a pointer to the
275release() method for this type of kobject. The other two fields (sysfs_ops
276and default_attrs) control how objects of this type are represented in
277sysfs; they are beyond the scope of this document.
235 278
236kobj_set_kset_s(obj,subsys) 279The default_attrs pointer is a list of default attributes that will be
280automatically created for any kobject that is registered with this ktype.
237 281
238- Assumes that obj->kobj exists, and is a struct kobject.
239- Sets the kset of that kobject to the kset <subsys>.
240 282
241int subsystem_register(struct kset *s); 283ksets
242void subsystem_unregister(struct kset *s);
243 284
244These are just wrappers around the respective kset_* functions. 285A kset is merely a collection of kobjects that want to be associated with
286each other. There is no restriction that they be of the same ktype, but be
287very careful if they are not.
245 288
2462.3 sysfs 289A kset serves these functions:
247 290
248ksets are represented in sysfs when their embedded kobjects are 291 - It serves as a bag containing a group of objects. A kset can be used by
249registered. They follow the same rules of parenting, with one 292 the kernel to track "all block devices" or "all PCI device drivers."
250exception. If a kset does not have a parent, nor is its embedded
251kobject part of another kset, the kset's parent becomes its dominant
252subsystem.
253 293
254If the kset does not have a parent, its directory is created at the 294 - A kset is also a subdirectory in sysfs, where the associated kobjects
255sysfs root. This should only happen when the kset registered is 295 with the kset can show up. Every kset contains a kobject which can be
256embedded in a subsystem itself. 296 set up to be the parent of other kobjects; the top-level directories of
297 the sysfs hierarchy are constructed in this way.
257 298
299 - Ksets can support the "hotplugging" of kobjects and influence how
300 uevent events are reported to user space.
258 301
2593. struct ktype 302In object-oriented terms, "kset" is the top-level container class; ksets
303contain their own kobject, but that kobject is managed by the kset code and
304should not be manipulated by any other user.
260 305
2613.1. Description 306A kset keeps its children in a standard kernel linked list. Kobjects point
307back to their containing kset via their kset field. In almost all cases,
308the kobjects belonging to a ket have that kset (or, strictly, its embedded
309kobject) in their parent.
262 310
263struct kobj_type { 311As a kset contains a kobject within it, it should always be dynamically
264 void (*release)(struct kobject *); 312created and never declared statically or on the stack. To create a new
265 struct sysfs_ops * sysfs_ops; 313kset use:
266 struct attribute ** default_attrs; 314 struct kset *kset_create_and_add(const char *name,
315 struct kset_uevent_ops *u,
316 struct kobject *parent);
317
318When you are finished with the kset, call:
319 void kset_unregister(struct kset *kset);
320to destroy it.
321
322An example of using a kset can be seen in the
323samples/kobject/kset-example.c file in the kernel tree.
324
325If a kset wishes to control the uevent operations of the kobjects
326associated with it, it can use the struct kset_uevent_ops to handle it:
327
328struct kset_uevent_ops {
329 int (*filter)(struct kset *kset, struct kobject *kobj);
330 const char *(*name)(struct kset *kset, struct kobject *kobj);
331 int (*uevent)(struct kset *kset, struct kobject *kobj,
332 struct kobj_uevent_env *env);
267}; 333};
268 334
269 335
270Object types require specific functions for converting between the 336The filter function allows a kset to prevent a uevent from being emitted to
271generic object and the more complex type. struct kobj_type provides 337userspace for a specific kobject. If the function returns 0, the uevent
272the object-specific fields, which include: 338will not be emitted.
339
340The name function will be called to override the default name of the kset
341that the uevent sends to userspace. By default, the name will be the same
342as the kset itself, but this function, if present, can override that name.
343
344The uevent function will be called when the uevent is about to be sent to
345userspace to allow more environment variables to be added to the uevent.
346
347One might ask how, exactly, a kobject is added to a kset, given that no
348functions which perform that function have been presented. The answer is
349that this task is handled by kobject_add(). When a kobject is passed to
350kobject_add(), its kset member should point to the kset to which the
351kobject will belong. kobject_add() will handle the rest.
352
353If the kobject belonging to a kset has no parent kobject set, it will be
354added to the kset's directory. Not all members of a kset do necessarily
355live in the kset directory. If an explicit parent kobject is assigned
356before the kobject is added, the kobject is registered with the kset, but
357added below the parent kobject.
358
359
360Kobject removal
273 361
274- release: Called when the kobject's reference count reaches 0. This 362After a kobject has been registered with the kobject core successfully, it
275 should convert the object to the more complex type and free it. 363must be cleaned up when the code is finished with it. To do that, call
364kobject_put(). By doing this, the kobject core will automatically clean up
365all of the memory allocated by this kobject. If a KOBJ_ADD uevent has been
366sent for the object, a corresponding KOBJ_REMOVE uevent will be sent, and
367any other sysfs housekeeping will be handled for the caller properly.
276 368
277- sysfs_ops: Provides conversion functions for sysfs access. Please 369If you need to do a two-stage delete of the kobject (say you are not
278 see the sysfs documentation for more information. 370allowed to sleep when you need to destroy the object), then call
371kobject_del() which will unregister the kobject from sysfs. This makes the
372kobject "invisible", but it is not cleaned up, and the reference count of
373the object is still the same. At a later time call kobject_put() to finish
374the cleanup of the memory associated with the kobject.
279 375
280- default_attrs: Default attributes to be exported via sysfs when the 376kobject_del() can be used to drop the reference to the parent object, if
281 object is registered.Note that the last attribute has to be 377circular references are constructed. It is valid in some cases, that a
282 initialized to NULL ! You can find a complete implementation 378parent objects references a child. Circular references _must_ be broken
283 in block/genhd.c 379with an explicit call to kobject_del(), so that a release functions will be
380called, and the objects in the former circle release each other.
284 381
285 382
286Instances of struct kobj_type are not registered; only referenced by 383Example code to copy from
287the kset. A kobj_type may be referenced by an arbitrary number of
288ksets, as there may be disparate sets of identical objects.
289 384
385For a more complete example of using ksets and kobjects properly, see the
386sample/kobject/kset-example.c code.
diff --git a/Documentation/pnp.txt b/Documentation/pnp.txt
index 481faf515d53..a327db67782a 100644
--- a/Documentation/pnp.txt
+++ b/Documentation/pnp.txt
@@ -17,9 +17,9 @@ The User Interface
17------------------ 17------------------
18 The Linux Plug and Play user interface provides a means to activate PnP devices 18 The Linux Plug and Play user interface provides a means to activate PnP devices
19for legacy and user level drivers that do not support Linux Plug and Play. The 19for legacy and user level drivers that do not support Linux Plug and Play. The
20user interface is integrated into driverfs. 20user interface is integrated into sysfs.
21 21
22In addition to the standard driverfs file the following are created in each 22In addition to the standard sysfs file the following are created in each
23device's directory: 23device's directory:
24id - displays a list of support EISA IDs 24id - displays a list of support EISA IDs
25options - displays possible resource configurations 25options - displays possible resource configurations
diff --git a/Documentation/s390/cds.txt b/Documentation/s390/cds.txt
index 3081927cc2d6..c4b7b2bd369a 100644
--- a/Documentation/s390/cds.txt
+++ b/Documentation/s390/cds.txt
@@ -133,7 +133,7 @@ During its startup the Linux/390 system checks for peripheral devices. Each
133of those devices is uniquely defined by a so called subchannel by the ESA/390 133of those devices is uniquely defined by a so called subchannel by the ESA/390
134channel subsystem. While the subchannel numbers are system generated, each 134channel subsystem. While the subchannel numbers are system generated, each
135subchannel also takes a user defined attribute, the so called device number. 135subchannel also takes a user defined attribute, the so called device number.
136Both subchannel number and device number cannot exceed 65535. During driverfs 136Both subchannel number and device number cannot exceed 65535. During sysfs
137initialisation, the information about control unit type and device types that 137initialisation, the information about control unit type and device types that
138imply specific I/O commands (channel command words - CCWs) in order to operate 138imply specific I/O commands (channel command words - CCWs) in order to operate
139the device are gathered. Device drivers can retrieve this set of hardware 139the device are gathered. Device drivers can retrieve this set of hardware
diff --git a/Documentation/vm/slabinfo.c b/Documentation/vm/slabinfo.c
index 7047696c47a1..488c1f31b992 100644
--- a/Documentation/vm/slabinfo.c
+++ b/Documentation/vm/slabinfo.c
@@ -1021,7 +1021,7 @@ void read_slab_dir(void)
1021 char *t; 1021 char *t;
1022 int count; 1022 int count;
1023 1023
1024 if (chdir("/sys/slab")) 1024 if (chdir("/sys/kernel/slab"))
1025 fatal("SYSFS support for SLUB not active\n"); 1025 fatal("SYSFS support for SLUB not active\n");
1026 1026
1027 dir = opendir("."); 1027 dir = opendir(".");
diff --git a/Documentation/vm/slub.txt b/Documentation/vm/slub.txt
index d17f324db9f5..dcf8bcf846d6 100644
--- a/Documentation/vm/slub.txt
+++ b/Documentation/vm/slub.txt
@@ -63,7 +63,7 @@ In case you forgot to enable debugging on the kernel command line: It is
63possible to enable debugging manually when the kernel is up. Look at the 63possible to enable debugging manually when the kernel is up. Look at the
64contents of: 64contents of:
65 65
66/sys/slab/<slab name>/ 66/sys/kernel/slab/<slab name>/
67 67
68Look at the writable files. Writing 1 to them will enable the 68Look at the writable files. Writing 1 to them will enable the
69corresponding debug option. All options can be set on a slab that does 69corresponding debug option. All options can be set on a slab that does
diff --git a/Documentation/zh_CN/CodingStyle b/Documentation/zh_CN/CodingStyle
new file mode 100644
index 000000000000..ecd9307a641f
--- /dev/null
+++ b/Documentation/zh_CN/CodingStyle
@@ -0,0 +1,701 @@
1Chinese translated version of Documentation/CodingStyle
2
3If you have any comment or update to the content, please post to LKML directly.
4However, if you have problem communicating in English you can also ask the
5Chinese maintainer for help. Contact the Chinese maintainer, if this
6translation is outdated or there is problem with translation.
7
8Chinese maintainer: Zhang Le <r0bertz@gentoo.org>
9---------------------------------------------------------------------
10Documentation/CodingStyle的中文翻译
11
12如果想评论或更新本文的内容,请直接发信到LKML。如果你使用英文交流有困难的话,也可
13以向中文版维护者求助。如果本翻译更新不及时或者翻译存在问题,请联系中文版维护者。
14
15中文版维护者: 张乐 Zhang Le <r0bertz@gentoo.org>
16中文版翻译者: 张乐 Zhang Le <r0bertz@gentoo.org>
17中文版校译者: 王聪 Wang Cong <xiyou.wangcong@gmail.com>
18 wheelz <kernel.zeng@gmail.com>
19 管旭东 Xudong Guan <xudong.guan@gmail.com>
20 Li Zefan <lizf@cn.fujitsu.com>
21 Wang Chen <wangchen@cn.fujitsu.com>
22以下为正文
23---------------------------------------------------------------------
24
25 Linux内核代码风格
26
27这是一个简短的文档,描述了linux内核的首选代码风格。代码风格是因人而异的,而且我
28不愿意把我的观点强加给任何人,不过这里所讲述的是我必须要维护的代码所遵守的风格,
29并且我也希望绝大多数其他代码也能遵守这个风格。请在写代码时至少考虑一下本文所述的
30风格。
31
32首先,我建议你打印一份GNU代码规范,然后不要读它。烧了它,这是一个具有重大象征性
33意义的动作。
34
35不管怎样,现在我们开始:
36
37
38 第一章:缩进
39
40制表符是8个字符,所以缩进也是8个字符。有些异端运动试图将缩进变为4(乃至2)个字符
41深,这几乎相当于尝试将圆周率的值定义为3。
42
43理由:缩进的全部意义就在于清楚的定义一个控制块起止于何处。尤其是当你盯着你的屏幕
44连续看了20小时之后,你将会发现大一点的缩进会使你更容易分辨缩进。
45
46现在,有些人会抱怨8个字符的缩进会使代码向右边移动的太远,在80个字符的终端屏幕上
47就很难读这样的代码。这个问题的答案是,如果你需要3级以上的缩进,不管用何种方式你
48的代码已经有问题了,应该修正你的程序。
49
50简而言之,8个字符的缩进可以让代码更容易阅读,还有一个好处是当你的函数嵌套太深的
51时候可以给你警告。留心这个警告。
52
53在switch语句中消除多级缩进的首选的方式是让“switch”和从属于它的“case”标签对齐于同
54一列,而不要“两次缩进”“case”标签。比如:
55
56 switch (suffix) {
57 case 'G':
58 case 'g':
59 mem <<= 30;
60 break;
61 case 'M':
62 case 'm':
63 mem <<= 20;
64 break;
65 case 'K':
66 case 'k':
67 mem <<= 10;
68 /* fall through */
69 default:
70 break;
71 }
72
73
74不要把多个语句放在一行里,除非你有什么东西要隐藏:
75
76 if (condition) do_this;
77 do_something_everytime;
78
79也不要在一行里放多个赋值语句。内核代码风格超级简单。就是避免可能导致别人误读的表
80达式。
81
82除了注释、文档和Kconfig之外,不要使用空格来缩进,前面的例子是例外,是有意为之。
83
84选用一个好的编辑器,不要在行尾留空格。
85
86
87 第二章:把长的行和字符串打散
88
89代码风格的意义就在于使用平常使用的工具来维持代码的可读性和可维护性。
90
91每一行的长度的限制是80列,我们强烈建议您遵守这个惯例。
92
93长于80列的语句要打散成有意义的片段。每个片段要明显短于原来的语句,而且放置的位置
94也明显的靠右。同样的规则也适用于有很长参数列表的函数头。长字符串也要打散成较短的
95字符串。唯一的例外是超过80列可以大幅度提高可读性并且不会隐藏信息的情况。
96
97void fun(int a, int b, int c)
98{
99 if (condition)
100 printk(KERN_WARNING "Warning this is a long printk with "
101 "3 parameters a: %u b: %u "
102 "c: %u \n", a, b, c);
103 else
104 next_statement;
105}
106
107 第三章:大括号和空格的放置
108
109C语言风格中另外一个常见问题是大括号的放置。和缩进大小不同,选择或弃用某种放置策
110略并没有多少技术上的原因,不过首选的方式,就像Kernighan和Ritchie展示给我们的,是
111把起始大括号放在行尾,而把结束大括号放在行首,所以:
112
113 if (x is true) {
114 we do y
115 }
116
117这适用于所有的非函数语句块(if、switch、for、while、do)。比如:
118
119 switch (action) {
120 case KOBJ_ADD:
121 return "add";
122 case KOBJ_REMOVE:
123 return "remove";
124 case KOBJ_CHANGE:
125 return "change";
126 default:
127 return NULL;
128 }
129
130不过,有一个例外,那就是函数:函数的起始大括号放置于下一行的开头,所以:
131
132 int function(int x)
133 {
134 body of function
135 }
136
137全世界的异端可能会抱怨这个不一致性是……呃……不一致的,不过所有思维健全的人都知道(
138a)K&R是_正确的_,并且(b)K&R是正确的。此外,不管怎样函数都是特殊的(在C语言中
139,函数是不能嵌套的)。
140
141注意结束大括号独自占据一行,除非它后面跟着同一个语句的剩余部分,也就是do语句中的
142“while”或者if语句中的“else”,像这样:
143
144 do {
145 body of do-loop
146 } while (condition);
147
148
149
150 if (x == y) {
151 ..
152 } else if (x > y) {
153 ...
154 } else {
155 ....
156 }
157
158理由:K&R。
159
160也请注意这种大括号的放置方式也能使空(或者差不多空的)行的数量最小化,同时不失可
161读性。因此,由于你的屏幕上的新行是不可再生资源(想想25行的终端屏幕),你将会有更
162多的空行来放置注释。
163
164当只有一个单独的语句的时候,不用加不必要的大括号。
165
166if (condition)
167 action();
168
169这点不适用于本身为某个条件语句的一个分支的单独语句。这时需要在两个分支里都使用大
170括号。
171
172if (condition) {
173 do_this();
174 do_that();
175} else {
176 otherwise();
177}
178
179 3.1:空格
180
181Linux内核的空格使用方式(主要)取决于它是用于函数还是关键字。(大多数)关键字后
182要加一个空格。值得注意的例外是sizeof、typeof、alignof和__attribute__,这些关键字
183某些程度上看起来更像函数(它们在Linux里也常常伴随小括号而使用,尽管在C语言里这样
184的小括号不是必需的,就像“struct fileinfo info”声明过后的“sizeof info”)。
185
186所以在这些关键字之后放一个空格:
187 if, switch, case, for, do, while
188但是不要在sizeof、typeof、alignof或者__attribute__这些关键字之后放空格。例如,
189 s = sizeof(struct file);
190
191不要在小括号里的表达式两侧加空格。这是一个反例:
192
193 s = sizeof( struct file );
194
195当声明指针类型或者返回指针类型的函数时,“*”的首选使用方式是使之靠近变量名或者函
196数名,而不是靠近类型名。例子:
197
198 char *linux_banner;
199 unsigned long long memparse(char *ptr, char **retptr);
200 char *match_strdup(substring_t *s);
201
202在大多数二元和三元操作符两侧使用一个空格,例如下面所有这些操作符:
203
204 = + - < > * / % | & ^ <= >= == != ? :
205
206但是一元操作符后不要加空格:
207 & * + - ~ ! sizeof typeof alignof __attribute__ defined
208
209后缀自加和自减一元操作符前不加空格:
210 ++ --
211
212前缀自加和自减一元操作符后不加空格:
213 ++ --
214
215“.”和“->”结构体成员操作符前后不加空格。
216
217不要在行尾留空白。有些可以自动缩进的编辑器会在新行的行首加入适量的空白,然后你
218就可以直接在那一行输入代码。不过假如你最后没有在那一行输入代码,有些编辑器就不
219会移除已经加入的空白,就像你故意留下一个只有空白的行。包含行尾空白的行就这样产
220生了。
221
222当git发现补丁包含了行尾空白的时候会警告你,并且可以应你的要求去掉行尾空白;不过
223如果你是正在打一系列补丁,这样做会导致后面的补丁失败,因为你改变了补丁的上下文。
224
225
226 第四章:命名
227
228C是一个简朴的语言,你的命名也应该这样。和Modula-2和Pascal程序员不同,C程序员不使
229用类似ThisVariableIsATemporaryCounter这样华丽的名字。C程序员会称那个变量为“tmp”
230,这样写起来会更容易,而且至少不会令其难于理解。
231
232不过,虽然混用大小写的名字是不提倡使用的,但是全局变量还是需要一个具描述性的名字
233。称一个全局函数为“foo”是一个难以饶恕的错误。
234
235全局变量(只有当你真正需要它们的时候再用它)需要有一个具描述性的名字,就像全局函
236数。如果你有一个可以计算活动用户数量的函数,你应该叫它“count_active_users()”或者
237类似的名字,你不应该叫它“cntuser()”。
238
239在函数名中包含函数类型(所谓的匈牙利命名法)是脑子出了问题——编译器知道那些类型而
240且能够检查那些类型,这样做只能把程序员弄糊涂了。难怪微软总是制造出有问题的程序。
241
242本地变量名应该简短,而且能够表达相关的含义。如果你有一些随机的整数型的循环计数器
243,它应该被称为“i”。叫它“loop_counter”并无益处,如果它没有被误解的可能的话。类似
244的,“tmp”可以用来称呼任意类型的临时变量。
245
246如果你怕混淆了你的本地变量名,你就遇到另一个问题了,叫做函数增长荷尔蒙失衡综合症
247。请看第六章(函数)。
248
249
250 第五章:Typedef
251
252不要使用类似“vps_t”之类的东西。
253
254对结构体和指针使用typedef是一个错误。当你在代码里看到:
255
256 vps_t a;
257
258这代表什么意思呢?
259
260相反,如果是这样
261
262 struct virtual_container *a;
263
264你就知道“a”是什么了。
265
266很多人认为typedef“能提高可读性”。实际不是这样的。它们只在下列情况下有用:
267
268 (a) 完全不透明的对象(这种情况下要主动使用typedef来隐藏这个对象实际上是什么)。
269
270 例如:“pte_t”等不透明对象,你只能用合适的访问函数来访问它们。
271
272 注意!不透明性和“访问函数”本身是不好的。我们使用pte_t等类型的原因在于真的是
273 完全没有任何共用的可访问信息。
274
275 (b) 清楚的整数类型,如此,这层抽象就可以帮助消除到底是“int”还是“long”的混淆。
276
277 u8/u16/u32是完全没有问题的typedef,不过它们更符合类别(d)而不是这里。
278
279 再次注意!要这样做,必须事出有因。如果某个变量是“unsigned long“,那么没有必要
280
281 typedef unsigned long myflags_t;
282
283 不过如果有一个明确的原因,比如它在某种情况下可能会是一个“unsigned int”而在
284 其他情况下可能为“unsigned long”,那么就不要犹豫,请务必使用typedef。
285
286 (c) 当你使用sparse按字面的创建一个新类型来做类型检查的时候。
287
288 (d) 和标准C99类型相同的类型,在某些例外的情况下。
289
290 虽然让眼睛和脑筋来适应新的标准类型比如“uint32_t”不需要花很多时间,可是有些
291 人仍然拒绝使用它们。
292
293 因此,Linux特有的等同于标准类型的“u8/u16/u32/u64”类型和它们的有符号类型是被
294 允许的——尽管在你自己的新代码中,它们不是强制要求要使用的。
295
296 当编辑已经使用了某个类型集的已有代码时,你应该遵循那些代码中已经做出的选择。
297
298 (e) 可以在用户空间安全使用的类型。
299
300 在某些用户空间可见的结构体里,我们不能要求C99类型而且不能用上面提到的“u32”
301 类型。因此,我们在与用户空间共享的所有结构体中使用__u32和类似的类型。
302
303可能还有其他的情况,不过基本的规则是永远不要使用typedef,除非你可以明确的应用上
304述某个规则中的一个。
305
306总的来说,如果一个指针或者一个结构体里的元素可以合理的被直接访问到,那么它们就不
307应该是一个typedef。
308
309
310 第六章:函数
311
312函数应该简短而漂亮,并且只完成一件事情。函数应该可以一屏或者两屏显示完(我们都知
313道ISO/ANSI屏幕大小是80x24),只做一件事情,而且把它做好。
314
315一个函数的最大长度是和该函数的复杂度和缩进级数成反比的。所以,如果你有一个理论上
316很简单的只有一个很长(但是简单)的case语句的函数,而且你需要在每个case里做很多很
317小的事情,这样的函数尽管很长,但也是可以的。
318
319不过,如果你有一个复杂的函数,而且你怀疑一个天分不是很高的高中一年级学生可能甚至
320搞不清楚这个函数的目的,你应该严格的遵守前面提到的长度限制。使用辅助函数,并为之
321取个具描述性的名字(如果你觉得它们的性能很重要的话,可以让编译器内联它们,这样的
322效果往往会比你写一个复杂函数的效果要好。)
323
324函数的另外一个衡量标准是本地变量的数量。此数量不应超过5-10个,否则你的函数就有
325问题了。重新考虑一下你的函数,把它分拆成更小的函数。人的大脑一般可以轻松的同时跟
326踪7个不同的事物,如果再增多的话,就会糊涂了。即便你聪颖过人,你也可能会记不清你2
327个星期前做过的事情。
328
329在源文件里,使用空行隔开不同的函数。如果该函数需要被导出,它的EXPORT*宏应该紧贴
330在它的结束大括号之下。比如:
331
332int system_is_up(void)
333{
334 return system_state == SYSTEM_RUNNING;
335}
336EXPORT_SYMBOL(system_is_up);
337
338在函数原型中,包含函数名和它们的数据类型。虽然C语言里没有这样的要求,在Linux里这
339是提倡的做法,因为这样可以很简单的给读者提供更多的有价值的信息。
340
341
342 第七章:集中的函数退出途径
343
344虽然被某些人声称已经过时,但是goto语句的等价物还是经常被编译器所使用,具体形式是
345无条件跳转指令。
346
347当一个函数从多个位置退出并且需要做一些通用的清理工作的时候,goto的好处就显现出来
348了。
349
350理由是:
351
352- 无条件语句容易理解和跟踪
353- 嵌套程度减小
354- 可以避免由于修改时忘记更新某个单独的退出点而导致的错误
355- 减轻了编译器的工作,无需删除冗余代码;)
356
357int fun(int a)
358{
359 int result = 0;
360 char *buffer = kmalloc(SIZE);
361
362 if (buffer == NULL)
363 return -ENOMEM;
364
365 if (condition1) {
366 while (loop1) {
367 ...
368 }
369 result = 1;
370 goto out;
371 }
372 ...
373out:
374 kfree(buffer);
375 return result;
376}
377
378 第八章:注释
379
380注释是好的,不过有过度注释的危险。永远不要在注释里解释你的代码是如何运作的:更好
381的做法是让别人一看你的代码就可以明白,解释写的很差的代码是浪费时间。
382
383一般的,你想要你的注释告诉别人你的代码做了什么,而不是怎么做的。也请你不要把注释
384放在一个函数体内部:如果函数复杂到你需要独立的注释其中的一部分,你很可能需要回到
385第六章看一看。你可以做一些小注释来注明或警告某些很聪明(或者槽糕)的做法,但不要
386加太多。你应该做的,是把注释放在函数的头部,告诉人们它做了什么,也可以加上它做这
387些事情的原因。
388
389当注释内核API函数时,请使用kernel-doc格式。请看
390Documentation/kernel-doc-nano-HOWTO.txt和scripts/kernel-doc以获得详细信息。
391
392Linux的注释风格是C89“/* ... */”风格。不要使用C99风格“// ...”注释。
393
394长(多行)的首选注释风格是:
395
396 /*
397 * This is the preferred style for multi-line
398 * comments in the Linux kernel source code.
399 * Please use it consistently.
400 *
401 * Description: A column of asterisks on the left side,
402 * with beginning and ending almost-blank lines.
403 */
404
405注释数据也是很重要的,不管是基本类型还是衍生类型。为了方便实现这一点,每一行应只
406声明一个数据(不要使用逗号来一次声明多个数据)。这样你就有空间来为每个数据写一段
407小注释来解释它们的用途了。
408
409
410 第九章:你已经把事情弄糟了
411
412这没什么,我们都是这样。可能你的使用了很长时间Unix的朋友已经告诉你“GNU emacs”能
413自动帮你格式化C源代码,而且你也注意到了,确实是这样,不过它所使用的默认值和我们
414想要的相去甚远(实际上,甚至比随机打的还要差——无数个猴子在GNU emacs里打字永远不
415会创造出一个好程序)(译注:请参考Infinite Monkey Theorem)
416
417所以你要么放弃GNU emacs,要么改变它让它使用更合理的设定。要采用后一个方案,你可
418以把下面这段粘贴到你的.emacs文件里。
419
420(defun linux-c-mode ()
421 "C mode with adjusted defaults for use with the Linux kernel."
422 (interactive)
423 (c-mode)
424 (c-set-style "K&R")
425 (setq tab-width 8)
426 (setq indent-tabs-mode t)
427 (setq c-basic-offset 8))
428
429这样就定义了M-x linux-c-mode命令。当你hack一个模块的时候,如果你把字符串
430-*- linux-c -*-放在头两行的某个位置,这个模式将会被自动调用。如果你希望在你修改
431/usr/src/linux里的文件时魔术般自动打开linux-c-mode的话,你也可能需要添加
432
433(setq auto-mode-alist (cons '("/usr/src/linux.*/.*\\.[ch]$" . linux-c-mode)
434 auto-mode-alist))
435
436到你的.emacs文件里。
437
438不过就算你尝试让emacs正确的格式化代码失败了,也并不意味着你失去了一切:还可以用“
439indent”。
440
441不过,GNU indent也有和GNU emacs一样有问题的设定,所以你需要给它一些命令选项。不
442过,这还不算太糟糕,因为就算是GNU indent的作者也认同K&R的权威性(GNU的人并不是坏
443人,他们只是在这个问题上被严重的误导了),所以你只要给indent指定选项“-kr -i8”
444(代表“K&R,8个字符缩进”),或者使用“scripts/Lindent”,这样就可以以最时髦的方式
445缩进源代码。
446
447“indent”有很多选项,特别是重新格式化注释的时候,你可能需要看一下它的手册页。不过
448记住:“indent”不能修正坏的编程习惯。
449
450
451 第十章:Kconfig配置文件
452
453对于遍布源码树的所有Kconfig*配置文件来说,它们缩进方式与C代码相比有所不同。紧挨
454在“config”定义下面的行缩进一个制表符,帮助信息则再多缩进2个空格。比如:
455
456config AUDIT
457 bool "Auditing support"
458 depends on NET
459 help
460 Enable auditing infrastructure that can be used with another
461 kernel subsystem, such as SELinux (which requires this for
462 logging of avc messages output). Does not do system-call
463 auditing without CONFIG_AUDITSYSCALL.
464
465仍然被认为不够稳定的功能应该被定义为依赖于“EXPERIMENTAL”:
466
467config SLUB
468 depends on EXPERIMENTAL && !ARCH_USES_SLAB_PAGE_STRUCT
469 bool "SLUB (Unqueued Allocator)"
470 ...
471
472而那些危险的功能(比如某些文件系统的写支持)应该在它们的提示字符串里显著的声明这
473一点:
474
475config ADFS_FS_RW
476 bool "ADFS write support (DANGEROUS)"
477 depends on ADFS_FS
478 ...
479
480要查看配置文件的完整文档,请看Documentation/kbuild/kconfig-language.txt。
481
482
483 第十一章:数据结构
484
485如果一个数据结构,在创建和销毁它的单线执行环境之外可见,那么它必须要有一个引用计
486数器。内核里没有垃圾收集(并且内核之外的垃圾收集慢且效率低下),这意味着你绝对需
487要记录你对这种数据结构的使用情况。
488
489引用计数意味着你能够避免上锁,并且允许多个用户并行访问这个数据结构——而不需要担心
490这个数据结构仅仅因为暂时不被使用就消失了,那些用户可能不过是沉睡了一阵或者做了一
491些其他事情而已。
492
493注意上锁不能取代引用计数。上锁是为了保持数据结构的一致性,而引用计数是一个内存管
494理技巧。通常二者都需要,不要把两个搞混了。
495
496很多数据结构实际上有2级引用计数,它们通常有不同“类”的用户。子类计数器统计子类用
497户的数量,每当子类计数器减至零时,全局计数器减一。
498
499这种“多级引用计数”的例子可以在内存管理(“struct mm_struct”:mm_users和mm_count)
500和文件系统(“struct super_block”:s_count和s_active)中找到。
501
502记住:如果另一个执行线索可以找到你的数据结构,但是这个数据结构没有引用计数器,这
503里几乎肯定是一个bug。
504
505
506 第十二章:宏,枚举和RTL
507
508用于定义常量的宏的名字及枚举里的标签需要大写。
509
510#define CONSTANT 0x12345
511
512在定义几个相关的常量时,最好用枚举。
513
514宏的名字请用大写字母,不过形如函数的宏的名字可以用小写字母。
515
516一般的,如果能写成内联函数就不要写成像函数的宏。
517
518含有多个语句的宏应该被包含在一个do-while代码块里:
519
520#define macrofun(a, b, c) \
521 do { \
522 if (a == 5) \
523 do_this(b, c); \
524 } while (0)
525
526使用宏的时候应避免的事情:
527
5281) 影响控制流程的宏:
529
530#define FOO(x) \
531 do { \
532 if (blah(x) < 0) \
533 return -EBUGGERED; \
534 } while(0)
535
536非常不好。它看起来像一个函数,不过却能导致“调用”它的函数退出;不要打乱读者大脑里
537的语法分析器。
538
5392) 依赖于一个固定名字的本地变量的宏:
540
541#define FOO(val) bar(index, val)
542
543可能看起来像是个不错的东西,不过它非常容易把读代码的人搞糊涂,而且容易导致看起来
544不相关的改动带来错误。
545
5463) 作为左值的带参数的宏: FOO(x) = y;如果有人把FOO变成一个内联函数的话,这种用
547法就会出错了。
548
5494) 忘记了优先级:使用表达式定义常量的宏必须将表达式置于一对小括号之内。带参数的
550宏也要注意此类问题。
551
552#define CONSTANT 0x4000
553#define CONSTEXP (CONSTANT | 3)
554
555cpp手册对宏的讲解很详细。Gcc internals手册也详细讲解了RTL(译注:register
556transfer language),内核里的汇编语言经常用到它。
557
558
559 第十三章:打印内核消息
560
561内核开发者应该是受过良好教育的。请一定注意内核信息的拼写,以给人以好的印象。不要
562用不规范的单词比如“dont”,而要用“do not”或者“don't”。保证这些信息简单、明了、无
563歧义。
564
565内核信息不必以句号(译注:英文句号,即点)结束。
566
567在小括号里打印数字(%d)没有任何价值,应该避免这样做。
568
569<linux/device.h>里有一些驱动模型诊断宏,你应该使用它们,以确保信息对应于正确的
570设备和驱动,并且被标记了正确的消息级别。这些宏有:dev_err(), dev_warn(),
571dev_info()等等。对于那些不和某个特定设备相关连的信息,<linux/kernel.h>定义了
572pr_debug()和pr_info()。
573
574写出好的调试信息可以是一个很大的挑战;当你写出来之后,这些信息在远程除错的时候
575就会成为极大的帮助。当DEBUG符号没有被定义的时候,这些信息不应该被编译进内核里
576(也就是说,默认地,它们不应该被包含在内)。如果你使用dev_dbg()或者pr_debug(),
577就能自动达到这个效果。很多子系统拥有Kconfig选项来启用-DDEBUG。还有一个相关的惯例
578是使用VERBOSE_DEBUG来添加dev_vdbg()消息到那些已经由DEBUG启用的消息之上。
579
580
581 第十四章:分配内存
582
583内核提供了下面的一般用途的内存分配函数:kmalloc(),kzalloc(),kcalloc()和
584vmalloc()。请参考API文档以获取有关它们的详细信息。
585
586传递结构体大小的首选形式是这样的:
587
588 p = kmalloc(sizeof(*p), ...);
589
590另外一种传递方式中,sizeof的操作数是结构体的名字,这样会降低可读性,并且可能会引
591入bug。有可能指针变量类型被改变时,而对应的传递给内存分配函数的sizeof的结果不变。
592
593强制转换一个void指针返回值是多余的。C语言本身保证了从void指针到其他任何指针类型
594的转换是没有问题的。
595
596
597 第十五章:内联弊病
598
599有一个常见的误解是内联函数是gcc提供的可以让代码运行更快的一个选项。虽然使用内联
600函数有时候是恰当的(比如作为一种替代宏的方式,请看第十二章),不过很多情况下不是
601这样。inline关键字的过度使用会使内核变大,从而使整个系统运行速度变慢。因为大内核
602会占用更多的指令高速缓存(译注:一级缓存通常是指令缓存和数据缓存分开的)而且会导
603致pagecache的可用内存减少。想象一下,一次pagecache未命中就会导致一次磁盘寻址,将
604耗时5毫秒。5毫秒的时间内CPU能执行很多很多指令。
605
606一个基本的原则是如果一个函数有3行以上,就不要把它变成内联函数。这个原则的一个例
607外是,如果你知道某个参数是一个编译时常量,而且因为这个常量你确定编译器在编译时能
608优化掉你的函数的大部分代码,那仍然可以给它加上inline关键字。kmalloc()内联函数就
609是一个很好的例子。
610
611人们经常主张给static的而且只用了一次的函数加上inline,如此不会有任何损失,因为没
612有什么好权衡的。虽然从技术上说这是正确的,但是实际上这种情况下即使不加inline gcc
613也可以自动使其内联。而且其他用户可能会要求移除inline,由此而来的争论会抵消inline
614自身的潜在价值,得不偿失。
615
616
617 第十六章:函数返回值及命名
618
619函数可以返回很多种不同类型的值,最常见的一种是表明函数执行成功或者失败的值。这样
620的一个值可以表示为一个错误代码整数(-Exxx=失败,0=成功)或者一个“成功”布尔值(
6210=失败,非0=成功)。
622
623混合使用这两种表达方式是难于发现的bug的来源。如果C语言本身严格区分整形和布尔型变
624量,那么编译器就能够帮我们发现这些错误……不过C语言不区分。为了避免产生这种bug,请
625遵循下面的惯例:
626
627 如果函数的名字是一个动作或者强制性的命令,那么这个函数应该返回错误代码整
628 数。如果是一个判断,那么函数应该返回一个“成功”布尔值。
629
630比如,“add work”是一个命令,所以add_work()函数在成功时返回0,在失败时返回-EBUSY。
631类似的,因为“PCI device present”是一个判断,所以pci_dev_present()函数在成功找到
632一个匹配的设备时应该返回1,如果找不到时应该返回0。
633
634所有导出(译注:EXPORT)的函数都必须遵守这个惯例,所有的公共函数也都应该如此。私
635有(static)函数不需要如此,但是我们也推荐这样做。
636
637返回值是实际计算结果而不是计算是否成功的标志的函数不受此惯例的限制。一般的,他们
638通过返回一些正常值范围之外的结果来表示出错。典型的例子是返回指针的函数,他们使用
639NULL或者ERR_PTR机制来报告错误。
640
641
642 第十七章:不要重新发明内核宏
643
644头文件include/linux/kernel.h包含了一些宏,你应该使用它们,而不要自己写一些它们的
645变种。比如,如果你需要计算一个数组的长度,使用这个宏
646
647 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
648
649类似的,如果你要计算某结构体成员的大小,使用
650
651 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
652
653还有可以做严格的类型检查的min()和max()宏,如果你需要可以使用它们。你可以自己看看
654那个头文件里还定义了什么你可以拿来用的东西,如果有定义的话,你就不应在你的代码里
655自己重新定义。
656
657
658 第十八章:编辑器模式行和其他需要罗嗦的事情
659
660有一些编辑器可以解释嵌入在源文件里的由一些特殊标记标明的配置信息。比如,emacs
661能够解释被标记成这样的行:
662
663-*- mode: c -*-
664
665或者这样的:
666
667/*
668Local Variables:
669compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c"
670End:
671*/
672
673Vim能够解释这样的标记:
674
675/* vim:set sw=8 noet */
676
677不要在源代码中包含任何这样的内容。每个人都有他自己的编辑器配置,你的源文件不应
678该覆盖别人的配置。这包括有关缩进和模式配置的标记。人们可以使用他们自己定制的模
679式,或者使用其他可以产生正确的缩进的巧妙方法。
680
681
682
683 附录 I:参考
684
685The C Programming Language, 第二版, 作者Brian W. Kernighan和Denni
686M. Ritchie. Prentice Hall, Inc., 1988. ISBN 0-13-110362-8 (软皮),
6870-13-110370-9 (硬皮). URL: http://cm.bell-labs.com/cm/cs/cbook/
688
689The Practice of Programming 作者Brian W. Kernighan和Rob Pike. Addison-Wesley,
690Inc., 1999. ISBN 0-201-61586-X. URL: http://cm.bell-labs.com/cm/cs/tpop/
691
692cpp,gcc,gcc internals和indent的GNU手册——和K&R及本文相符合的部分,全部可以在
693http://www.gnu.org/manual/找到
694
695WG14是C语言的国际标准化工作组,URL: http://www.open-std.org/JTC1/SC22/WG14/
696
697Kernel CodingStyle,作者greg@kroah.com发表于OLS 2002:
698http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/
699
700--
701最后更新于2007年7月13日。
diff --git a/Documentation/zh_CN/HOWTO b/Documentation/zh_CN/HOWTO
index 48fc67bfbe3d..3d80e8af36ec 100644
--- a/Documentation/zh_CN/HOWTO
+++ b/Documentation/zh_CN/HOWTO
@@ -1,10 +1,10 @@
1Chinese translated version of Documentation/HOWTO 1Chinese translated version of Documentation/HOWTO
2 2
3If you have any comment or update to the content, please contact the 3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have problem 4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for 5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer, if this translation is outdated 6help. Contact the Chinese maintainer if this translation is outdated
7or there is problem with translation. 7or if there is a problem with the translation.
8 8
9Maintainer: Greg Kroah-Hartman <greg@kroah.com> 9Maintainer: Greg Kroah-Hartman <greg@kroah.com>
10Chinese maintainer: Li Yang <leoli@freescale.com> 10Chinese maintainer: Li Yang <leoli@freescale.com>
@@ -85,7 +85,7 @@ Linux内核源代码都是在GPL(通用公共许可证)的保护下发布的
85Linux内核代码中包含有大量的文档。这些文档对于学习如何与内核社区互动有着 85Linux内核代码中包含有大量的文档。这些文档对于学习如何与内核社区互动有着
86不可估量的价值。当一个新的功能被加入内核,最好把解释如何使用这个功能的文 86不可估量的价值。当一个新的功能被加入内核,最好把解释如何使用这个功能的文
87档也放进内核。当内核的改动导致面向用户空间的接口发生变化时,最好将相关信 87档也放进内核。当内核的改动导致面向用户空间的接口发生变化时,最好将相关信
88息或手册页(manpages)的补丁发到mtk-manpages@gmx.net,以向手册页(manpages) 88息或手册页(manpages)的补丁发到mtk.manpages@gmail.com,以向手册页(manpages)
89的维护者解释这些变化。 89的维护者解释这些变化。
90 90
91以下是内核代码中需要阅读的文档: 91以下是内核代码中需要阅读的文档:
@@ -218,6 +218,8 @@ kernel.org网站的pub/linux/kernel/v2.6/目录下找到它。它的开发遵循
218 时,一个新的-rc版本就会被发布。计划是每周都发布新的-rc版本。 218 时,一个新的-rc版本就会被发布。计划是每周都发布新的-rc版本。
219 - 这个过程一直持续下去直到内核被认为达到足够稳定的状态,持续时间大概是 219 - 这个过程一直持续下去直到内核被认为达到足够稳定的状态,持续时间大概是
220 6个星期。 220 6个星期。
221 - 以下地址跟踪了在每个-rc发布中发现的退步列表:
222 http://kernelnewbies.org/known_regressions
221 223
222关于内核发布,值得一提的是Andrew Morton在linux-kernel邮件列表中如是说: 224关于内核发布,值得一提的是Andrew Morton在linux-kernel邮件列表中如是说:
223 “没有人知道新内核何时会被发布,因为发布是根据已知bug的情况来决定 225 “没有人知道新内核何时会被发布,因为发布是根据已知bug的情况来决定
diff --git a/Documentation/zh_CN/SubmittingDrivers b/Documentation/zh_CN/SubmittingDrivers
new file mode 100644
index 000000000000..5f4815c63ec7
--- /dev/null
+++ b/Documentation/zh_CN/SubmittingDrivers
@@ -0,0 +1,168 @@
1Chinese translated version of Documentation/SubmittingDrivers
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Chinese maintainer: Li Yang <leo@zh-kernel.org>
10---------------------------------------------------------------------
11Documentation/SubmittingDrivers 的中文翻译
12
13如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
14交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
15译存在问题,请联系中文版维护者。
16
17中文版维护者: 李阳 Li Yang <leo@zh-kernel.org>
18中文版翻译者: 李阳 Li Yang <leo@zh-kernel.org>
19中文版校译者: 陈琦 Maggie Chen <chenqi@beyondsoft.com>
20 王聪 Wang Cong <xiyou.wangcong@gmail.com>
21 张巍 Zhang Wei <Wei.Zhang@freescale.com>
22
23以下为正文
24---------------------------------------------------------------------
25
26如何向 Linux 内核提交驱动程序
27-----------------------------
28
29这篇文档将会解释如何向不同的内核源码树提交设备驱动程序。请注意,如果你感
30兴趣的是显卡驱动程序,你也许应该访问 XFree86 项目(http://www.xfree86.org/)
31和/或 X.org 项目 (http://x.org)。
32
33另请参阅 Documentation/SubmittingPatches 文档。
34
35
36分配设备号
37----------
38
39块设备和字符设备的主设备号与从设备号是由 Linux 命名编号分配权威 LANANA(
40现在是 Torben Mathiasen)负责分配。申请的网址是 http://www.lanana.org/。
41即使不准备提交到主流内核的设备驱动也需要在这里分配设备号。有关详细信息,
42请参阅 Documentation/devices.txt。
43
44如果你使用的不是已经分配的设备号,那么当你提交设备驱动的时候,它将会被强
45制分配一个新的设备号,即便这个设备号和你之前发给客户的截然不同。
46
47设备驱动的提交对象
48------------------
49
50Linux 2.0:
51 此内核源码树不接受新的驱动程序。
52
53Linux 2.2:
54 此内核源码树不接受新的驱动程序。
55
56Linux 2.4:
57 如果所属的代码领域在内核的 MAINTAINERS 文件中列有一个总维护者,
58 那么请将驱动程序提交给他。如果此维护者没有回应或者你找不到恰当的
59 维护者,那么请联系 Willy Tarreau <w@1wt.eu>。
60
61Linux 2.6:
62 除了遵循和 2.4 版内核同样的规则外,你还需要在 linux-kernel 邮件
63 列表上跟踪最新的 API 变化。向 Linux 2.6 内核提交驱动的顶级联系人
64 是 Andrew Morton <akpm@osdl.org>。
65
66决定设备驱动能否被接受的条件
67----------------------------
68
69许可: 代码必须使用 GNU 通用公开许可证 (GPL) 提交给 Linux,但是
70 我们并不要求 GPL 是唯一的许可。你或许会希望同时使用多种
71 许可证发布,如果希望驱动程序可以被其他开源社区(比如BSD)
72 使用。请参考 include/linux/module.h 文件中所列出的可被
73 接受共存的许可。
74
75版权: 版权所有者必须同意使用 GPL 许可。最好提交者和版权所有者
76 是相同个人或实体。否则,必需列出授权使用 GPL 的版权所有
77 人或实体,以备验证之需。
78
79接口: 如果你的驱动程序使用现成的接口并且和其他同类的驱动程序行
80 为相似,而不是去发明无谓的新接口,那么它将会更容易被接受。
81 如果你需要一个 Linux 和 NT 的通用驱动接口,那么请在用
82 户空间实现它。
83
84代码: 请使用 Documentation/CodingStyle 中所描述的 Linux 代码风
85 格。如果你的某些代码段(例如那些与 Windows 驱动程序包共
86 享的代码段)需要使用其他格式,而你却只希望维护一份代码,
87 那么请将它们很好地区分出来,并且注明原因。
88
89可移植性: 请注意,指针并不永远是 32 位的,不是所有的计算机都使用小
90 尾模式 (little endian) 存储数据,不是所有的人都拥有浮点
91 单元,不要随便在你的驱动程序里嵌入 x86 汇编指令。只能在
92 x86 上运行的驱动程序一般是不受欢迎的。虽然你可能只有 x86
93 硬件,很难测试驱动程序在其他平台上是否可用,但是确保代码
94 可以被轻松地移植却是很简单的。
95
96清晰度: 做到所有人都能修补这个驱动程序将会很有好处,因为这样你将
97 会直接收到修复的补丁而不是 bug 报告。如果你提交一个试图
98 隐藏硬件工作机理的驱动程序,那么它将会被扔进废纸篓。
99
100电源管理: 因为 Linux 正在被很多移动设备和桌面系统使用,所以你的驱
101 动程序也很有可能被使用在这些设备上。它应该支持最基本的电
102 源管理,即在需要的情况下实现系统级休眠和唤醒要用到的
103 .suspend 和 .resume 函数。你应该检查你的驱动程序是否能正
104 确地处理休眠与唤醒,如果实在无法确认,请至少把 .suspend
105 函数定义成返回 -ENOSYS(功能未实现)错误。你还应该尝试确
106 保你的驱动在什么都不干的情况下将耗电降到最低。要获得驱动
107 程序测试的指导,请参阅
108 Documentation/power/drivers-testing.txt。有关驱动程序电
109 源管理问题相对全面的概述,请参阅
110 Documentation/power/devices.txt。
111
112管理: 如果一个驱动程序的作者还在进行有效的维护,那么通常除了那
113 些明显正确且不需要任何检查的补丁以外,其他所有的补丁都会
114 被转发给作者。如果你希望成为驱动程序的联系人和更新者,最
115 好在代码注释中写明并且在 MAINTAINERS 文件中加入这个驱动
116 程序的条目。
117
118不影响设备驱动能否被接受的条件
119------------------------------
120
121供应商: 由硬件供应商来维护驱动程序通常是一件好事。不过,如果源码
122 树里已经有其他人提供了可稳定工作的驱动程序,那么请不要期
123 望“我是供应商”会成为内核改用你的驱动程序的理由。理想的情
124 况是:供应商与现有驱动程序的作者合作,构建一个统一完美的
125 驱动程序。
126
127作者: 驱动程序是由大的 Linux 公司研发还是由你个人编写,并不影
128 响其是否能被内核接受。没有人对内核源码树享有特权。只要你
129 充分了解内核社区,你就会发现这一点。
130
131
132资源列表
133--------
134
135Linux 内核主源码树:
136 ftp.??.kernel.org:/pub/linux/kernel/...
137 ?? == 你的国家代码,例如 "cn"、"us"、"uk"、"fr" 等等
138
139Linux 内核邮件列表:
140 linux-kernel@vger.kernel.org
141 [可通过向majordomo@vger.kernel.org发邮件来订阅]
142
143Linux 设备驱动程序,第三版(探讨 2.6.10 版内核):
144 http://lwn.net/Kernel/LDD3/ (免费版)
145
146LWN.net:
147 每周内核开发活动摘要 - http://lwn.net/
148 2.6 版中 API 的变更:
149 http://lwn.net/Articles/2.6-kernel-api/
150 将旧版内核的驱动程序移植到 2.6 版:
151 http://lwn.net/Articles/driver-porting/
152
153KernelTrap:
154 Linux 内核的最新动态以及开发者访谈
155 http://kerneltrap.org/
156
157内核新手(KernelNewbies):
158 为新的内核开发者提供文档和帮助
159 http://kernelnewbies.org/
160
161Linux USB项目:
162 http://www.linux-usb.org/
163
164写内核驱动的“不要”(Arjan van de Ven著):
165 http://www.fenrus.org/how-to-not-write-a-device-driver-paper.pdf
166
167内核清洁工 (Kernel Janitor):
168 http://janitor.kernelnewbies.org/
diff --git a/Documentation/zh_CN/SubmittingPatches b/Documentation/zh_CN/SubmittingPatches
new file mode 100644
index 000000000000..985c92e20b73
--- /dev/null
+++ b/Documentation/zh_CN/SubmittingPatches
@@ -0,0 +1,416 @@
1Chinese translated version of Documentation/SubmittingPatches
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Chinese maintainer: TripleX Chung <triplex@zh-kernel.org>
10---------------------------------------------------------------------
11Documentation/SubmittingPatches 的中文翻译
12
13如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
14交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
15译存在问题,请联系中文版维护者。
16
17中文版维护者: 钟宇 TripleX Chung <triplex@zh-kernel.org>
18中文版翻译者: 钟宇 TripleX Chung <triplex@zh-kernel.org>
19中文版校译者: 李阳 Li Yang <leo@zh-kernel.org>
20 王聪 Wang Cong <xiyou.wangcong@gmail.com>
21
22以下为正文
23---------------------------------------------------------------------
24
25 如何让你的改动进入内核
26 或者
27 获得亲爱的 Linus Torvalds 的关注和处理
28----------------------------------
29
30对于想要将改动提交到 Linux 内核的个人或者公司来说,如果不熟悉“规矩”,
31提交的流程会让人畏惧。本文档收集了一系列建议,这些建议可以大大的提高你
32的改动被接受的机会。
33阅读 Documentation/SubmitChecklist 来获得在提交代码前需要检查的项目的列
34表。如果你在提交一个驱动程序,那么同时阅读一下
35Documentation/SubmittingDrivers 。
36
37
38--------------------------
39第一节 - 创建并发送你的改动
40--------------------------
41
421) "diff -up"
43-----------
44
45使用 "diff -up" 或者 "diff -uprN" 来创建补丁。
46
47所有内核的改动,都是以补丁的形式呈现的,补丁由 diff(1) 生成。创建补丁的
48时候,要确认它是以 "unified diff" 格式创建的,这种格式由 diff(1) 的 '-u'
49参数生成。而且,请使用 '-p' 参数,那样会显示每个改动所在的C函数,使得
50产生的补丁容易读得多。补丁应该基于内核源代码树的根目录,而不是里边的任
51何子目录。
52为一个单独的文件创建补丁,一般来说这样做就够了:
53
54 SRCTREE= linux-2.6
55 MYFILE= drivers/net/mydriver.c
56
57 cd $SRCTREE
58 cp $MYFILE $MYFILE.orig
59 vi $MYFILE # make your change
60 cd ..
61 diff -up $SRCTREE/$MYFILE{.orig,} > /tmp/patch
62
63为多个文件创建补丁,你可以解开一个没有修改过的内核源代码树,然后和你自
64己的代码树之间做 diff 。例如:
65
66 MYSRC= /devel/linux-2.6
67
68 tar xvfz linux-2.6.12.tar.gz
69 mv linux-2.6.12 linux-2.6.12-vanilla
70 diff -uprN -X linux-2.6.12-vanilla/Documentation/dontdiff \
71 linux-2.6.12-vanilla $MYSRC > /tmp/patch
72
73"dontdiff" 是内核在编译的时候产生的文件的列表,列表中的文件在 diff(1)
74产生的补丁里会被跳过。"dontdiff" 文件被包含在2.6.12和之后版本的内核源代
75码树中。对于更早的内核版本,你可以从
76<http://www.xenotime.net/linux/doc/dontdiff> 获取它。
77确定你的补丁里没有包含任何不属于这次补丁提交的额外文件。记得在用diff(1)
78生成补丁之后,审阅一次补丁,以确保准确。
79如果你的改动很散乱,你应该研究一下如何将补丁分割成独立的部分,将改动分
80割成一系列合乎逻辑的步骤。这样更容易让其他内核开发者审核,如果你想你的
81补丁被接受,这是很重要的。下面这些脚本能够帮助你做这件事情:
82Quilt:
83http://savannah.nongnu.org/projects/quilt
84
85Andrew Morton 的补丁脚本:
86http://www.zip.com.au/~akpm/linux/patches/
87作为这些脚本的替代,quilt 是值得推荐的补丁管理工具(看上面的链接)。
88
892)描述你的改动。
90描述你的改动包含的技术细节。
91
92要多具体就写多具体。最糟糕的描述可能是像下面这些语句:“更新了某驱动程
93序”,“修正了某驱动程序的bug”,或者“这个补丁包含了某子系统的修改,请
94使用。”
95
96如果你的描述开始变长,这表示你也许需要拆分你的补丁了,请看第3小节,
97继续。
98
993)拆分你的改动
100
101将改动拆分,逻辑类似的放到同一个补丁文件里。
102
103例如,如果你的改动里同时有bug修正和性能优化,那么把这些改动才分到两个或
104者更多的补丁文件中。如果你的改动包含对API的修改,并且修改了驱动程序来适
105应这些新的API,那么把这些修改分成两个补丁。
106
107另一方面,如果你将一个单独的改动做成多个补丁文件,那么将它们合并成一个
108单独的补丁文件。这样一个逻辑上单独的改动只被包含在一个补丁文件里。
109
110如果有一个补丁依赖另外一个补丁来完成它的改动,那没问题。简单的在你的补
111丁描述里指出“这个补丁依赖某补丁”就好了。
112
113如果你不能将补丁浓缩成更少的文件,那么每次大约发送出15个,然后等待审查
114和整合。
115
1164)选择 e-mail 的收件人
117
118看一遍 MAINTAINERS 文件和源代码,看看你所的改动所在的内核子系统有没有指
119定的维护者。如果有,给他们发e-mail。
120
121如果没有找到维护者,或者维护者没有反馈,将你的补丁发送到内核开发者主邮
122件列表 linux-kernel@vger.kernel.org。大部分的内核开发者都跟踪这个邮件列
123表,可以评价你的改动。
124
125每次不要发送超过15个补丁到 vger 邮件列表!!!
126
127Linus Torvalds 是决定改动能否进入 Linux 内核的最终裁决者。他的 e-mail
128地址是 <torvalds@linux-foundation.org> 。他收到的 e-mail 很多,所以一般
129的说,最好别给他发 e-mail。
130
131那些修正bug,“显而易见”的修改或者是类似的只需要很少讨论的补丁可以直接
132发送或者CC给Linus。那些需要讨论或者没有很清楚的好处的补丁,一般先发送到
133linux-kernel邮件列表。只有当补丁被讨论得差不多了,才提交给Linus。
134
1355)选择CC( e-mail 抄送)列表
136
137除非你有理由不这样做,否则CC linux-kernel@vger.kernel.org。
138
139除了 Linus 之外,其他内核开发者也需要注意到你的改动,这样他们才能评论你
140的改动并提供代码审查和建议。linux-kernel 是 Linux 内核开发者主邮件列表
141。其它的邮件列表为特定的子系统提供服务,比如 USB,framebuffer 设备,虚
142拟文件系统,SCSI 子系统,等等。查看 MAINTAINERS 文件来获得和你的改动有
143关的邮件列表。
144
145Majordomo lists of VGER.KERNEL.ORG at:
146 <http://vger.kernel.org/vger-lists.html>
147
148如果改动影响了用户空间和内核之间的接口,请给 MAN-PAGES 的维护者(列在
149MAITAINERS 文件里的)发送一个手册页(man-pages)补丁,或者至少通知一下改
150变,让一些信息有途径进入手册页。
151
152即使在第四步的时候,维护者没有作出回应,也要确认在修改他们的代码的时候
153,一直将维护者拷贝到CC列表中。
154
155对于小的补丁,你也许会CC到 Adrian Bunk 管理的搜集琐碎补丁的邮件列表
156(Trivial Patch Monkey)trivial@kernel.org,那里专门收集琐碎的补丁。下面这样
157的补丁会被看作“琐碎的”补丁:
158 文档的拼写修正。
159 修正会影响到 grep(1) 的拼写。
160 警告信息修正(频繁的打印无用的警告是不好的。)
161 编译错误修正(代码逻辑的确是对的,只是编译有问题。)
162 运行时修正(只要真的修正了错误。)
163 移除使用了被废弃的函数/宏的代码(例如 check_region。)
164 联系方式和文档修正。
165 用可移植的代码替换不可移植的代码(即使在体系结构相关的代码中,既然有
166 人拷贝,只要它是琐碎的)
167 任何文件的作者/维护者对该文件的改动(例如 patch monkey 在重传模式下)
168
169URL: <http://www.kernel.org/pub/linux/kernel/people/bunk/trivial/>
170
171(译注,关于“琐碎补丁”的一些说明:因为原文的这一部分写得比较简单,所以不得不
172违例写一下译注。"trivial"这个英文单词的本意是“琐碎的,不重要的。”但是在这里
173有稍微有一些变化,例如对一些明显的NULL指针的修正,属于运行时修正,会被归类
174到琐碎补丁里。虽然NULL指针的修正很重要,但是这样的修正往往很小而且很容易得到
175检验,所以也被归入琐碎补丁。琐碎补丁更精确的归类应该是
176“simple, localized & easy to verify”,也就是说简单的,局部的和易于检验的。
177trivial@kernel.org邮件列表的目的是针对这样的补丁,为提交者提供一个中心,来
178降低提交的门槛。)
179
1806)没有 MIME 编码,没有链接,没有压缩,没有附件,只有纯文本。
181
182Linus 和其他的内核开发者需要阅读和评论你提交的改动。对于内核开发者来说
183,可以“引用”你的改动很重要,使用一般的 e-mail 工具,他们就可以在你的
184代码的任何位置添加评论。
185
186因为这个原因,所有的提交的补丁都是 e-mail 中“内嵌”的。
187警告:如果你使用剪切-粘贴你的补丁,小心你的编辑器的自动换行功能破坏你的
188补丁。
189
190不要将补丁作为 MIME 编码的附件,不管是否压缩。很多流行的 e-mail 软件不
191是任何时候都将 MIME 编码的附件当作纯文本发送的,这会使得别人无法在你的
192代码中加评论。另外,MIME 编码的附件会让 Linus 多花一点时间来处理,这就
193降低了你的改动被接受的可能性。
194
195警告:一些邮件软件,比如 Mozilla 会将你的信息以如下格式发送:
196---- 邮件头 ----
197Content-Type: text/plain; charset=us-ascii; format=flowed
198---- 邮件头 ----
199问题在于 “format=flowed” 会让接收端的某些邮件软件将邮件中的制表符替换
200成空格以及做一些类似的替换。这样,你发送的时候看起来没问题的补丁就被破
201坏了。
202
203要修正这个问题,只需要将你的 mozilla 的 defaults/pref/mailnews.js 文件
204里的
205pref("mailnews.send_plaintext_flowed", false); // RFC 2646=======
206修改成
207pref("mailnews.display.disable_format_flowed_support", true);
208就可以了。
209
2107) e-mail 的大小
211
212给 Linus 发送补丁的时候,永远按照第6小节说的做。
213
214大的改动对邮件列表不合适,对某些维护者也不合适。如果你的补丁,在不压缩
215的情况下,超过了40kB,那么你最好将补丁放在一个能通过 internet 访问的服
216务器上,然后用指向你的补丁的 URL 替代。
217
2188) 指出你的内核版本
219
220在标题和在补丁的描述中,指出补丁对应的内核的版本,是很重要的。
221
222如果补丁不能干净的在最新版本的内核上打上,Linus 是不会接受它的。
223
2249) 不要气馁,继续提交。
225
226当你提交了改动以后,耐心地等待。如果 Linus 喜欢你的改动并且同意它,那么
227它将在下一个内核发布版本中出现。
228
229然而,如果你的改动没有出现在下一个版本的内核中,可能有若干原因。减少那
230些原因,修正错误,重新提交更新后的改动,是你自己的工作。
231
232Linus不给出任何评论就“丢弃”你的补丁是常见的事情。在系统中这样的事情很
233平常。如果他没有接受你的补丁,也许是由于以下原本:
234* 你的补丁不能在最新版本的内核上干净的打上。
235* 你的补丁在 linux-kernel 邮件列表中没有得到充分的讨论。
236* 风格问题(参照第2小节)
237* 邮件格式问题(重读本节)
238* 你的改动有技术问题。
239* 他收到了成吨的 e-mail,而你的在混乱中丢失了。
240* 你让人为难。
241
242有疑问的时候,在 linux-kernel 邮件列表上请求评论。
243
24410) 在标题上加上 PATCH 的字样
245
246Linus 和 linux-kernel 邮件列表的 e-mail 流量都很高,一个通常的约定是标
247题行以 [PATCH] 开头。这样可以让 Linus 和其他内核开发人员可以从 e-mail
248的讨论中很轻易的将补丁分辨出来。
249
25011)为你的工作签名
251
252为了加强对谁做了何事的追踪,尤其是对那些透过好几层的维护者的补丁,我们
253建议在发送出去的补丁上加一个 “sign-off” 的过程。
254
255"sign-off" 是在补丁的注释的最后的简单的一行文字,认证你编写了它或者其他
256人有权力将它作为开放源代码的补丁传递。规则很简单:如果你能认证如下信息
257
258 开发者来源证书 1.1
259 对于本项目的贡献,我认证如下信息:
260 (a)这些贡献是完全或者部分的由我创建,我有权利以文件中指出
261 的开放源代码许可证提交它;或者
262 (b)这些贡献基于以前的工作,据我所知,这些以前的工作受恰当的开放
263 源代码许可证保护,而且,根据许可证,我有权提交修改后的贡献,
264 无论是完全还是部分由我创造,这些贡献都使用同一个开放源代码许可证
265 (除非我被允许用其它的许可证),正如文件中指出的;或者
266 (c)这些贡献由认证(a),(b)或者(c)的人直接提供给我,而
267 且我没有修改它。
268 (d)我理解并同意这个项目和贡献是公开的,贡献的记录(包括我
269 一起提交的个人记录,包括 sign-off )被永久维护并且可以和这个项目
270 或者开放源代码的许可证同步地再发行。
271 那么加入这样一行:
272 Signed-off-by: Random J Developer <random@developer.example.org>
273
274使用你的真名(抱歉,不能使用假名或者匿名。)
275
276有人在最后加上标签。现在这些东西会被忽略,但是你可以这样做,来标记公司
277内部的过程,或者只是指出关于 sign-off 的一些特殊细节。
278
27912)标准补丁格式
280
281标准的补丁,标题行是:
282 Subject: [PATCH 001/123] 子系统:一句话概述
283
284标准补丁的信体存在如下部分:
285
286 - 一个 "from" 行指出补丁作者。
287
288 - 一个空行
289
290 - 说明的主体,这些说明文字会被拷贝到描述该补丁的永久改动记录里。
291
292 - 一个由"---"构成的标记行
293
294 - 不合适放到改动记录里的额外的注解。
295
296 - 补丁本身(diff 输出)
297
298标题行的格式,使得对标题行按字母序排序非常的容易 - 很多 e-mail 客户端都
299可以支持 - 因为序列号是用零填充的,所以按数字排序和按字母排序是一样的。
300
301e-mail 标题中的“子系统”标识哪个内核子系统将被打补丁。
302
303e-mail 标题中的“一句话概述”扼要的描述 e-mail 中的补丁。“一句话概述”
304不应该是一个文件名。对于一个补丁系列(“补丁系列”指一系列的多个相关补
305丁),不要对每个补丁都使用同样的“一句话概述”。
306
307记住 e-mail 的“一句话概述”会成为该补丁的全局唯一标识。它会蔓延到 git
308的改动记录里。然后“一句话概述”会被用在开发者的讨论里,用来指代这个补
309丁。用户将希望通过 google 来搜索"一句话概述"来找到那些讨论这个补丁的文
310章。
311
312一些标题的例子:
313
314 Subject: [patch 2/5] ext2: improve scalability of bitmap searching
315 Subject: [PATCHv2 001/207] x86: fix eflags tracking
316
317"from" 行是信体里的最上面一行,具有如下格式:
318 From: Original Author <author@example.com>
319
320"from" 行指明在永久改动日志里,谁会被确认为作者。如果没有 "from" 行,那
321么邮件头里的 "From: " 行会被用来决定改动日志中的作者。
322
323说明的主题将会被提交到永久的源代码改动日志里,因此对那些早已经不记得和
324这个补丁相关的讨论细节的有能力的读者来说,是有意义的。
325
326"---" 标记行对于补丁处理工具要找到哪里是改动日志信息的结束,是不可缺少
327的。
328
329对于 "---" 标记之后的额外注解,一个好的用途就是用来写 diffstat,用来显
330示修改了什么文件和每个文件都增加和删除了多少行。diffstat 对于比较大的补
331丁特别有用。其余那些只是和时刻或者开发者相关的注解,不合适放到永久的改
332动日志里的,也应该放这里。
333使用 diffstat的选项 "-p 1 -w 70" 这样文件名就会从内核源代码树的目录开始
334,不会占用太宽的空间(很容易适合80列的宽度,也许会有一些缩进。)
335
336在后面的参考资料中能看到适当的补丁格式的更多细节。
337
338-------------------------------
339第二节 提示,建议和诀窍
340-------------------------------
341
342本节包含很多和提交到内核的代码有关的通常的"规则"。事情永远有例外...但是
343你必须真的有好的理由这样做。你可以把本节叫做Linus的计算机科学入门课。
344
3451) 读 Document/CodingStyle
346
347Nuff 说过,如果你的代码和这个偏离太多,那么它有可能会被拒绝,没有更多的
348审查,没有更多的评价。
349
3502) #ifdef 是丑陋的
351混杂了 ifdef 的代码难以阅读和维护。别这样做。作为替代,将你的 ifdef 放
352在头文件里,有条件地定义 "static inline" 函数,或者宏,在代码里用这些东
353西。让编译器把那些"空操作"优化掉。
354
355一个简单的例子,不好的代码:
356
357 dev = alloc_etherdev (sizeof(struct funky_private));
358 if (!dev)
359 return -ENODEV;
360 #ifdef CONFIG_NET_FUNKINESS
361 init_funky_net(dev);
362 #endif
363
364清理后的例子:
365
366(头文件里)
367 #ifndef CONFIG_NET_FUNKINESS
368 static inline void init_funky_net (struct net_device *d) {}
369 #endif
370
371(代码文件里)
372 dev = alloc_etherdev (sizeof(struct funky_private));
373 if (!dev)
374 return -ENODEV;
375 init_funky_net(dev);
376
3773) 'static inline' 比宏好
378
379Static inline 函数相比宏来说,是好得多的选择。Static inline 函数提供了
380类型安全,没有长度限制,没有格式限制,在 gcc 下开销和宏一样小。
381
382宏只在 static inline 函数不是最优的时候[在 fast paths 里有很少的独立的
383案例],或者不可能用 static inline 函数的时候[例如字符串分配]。
384应该用 'static inline' 而不是 'static __inline__', 'extern inline' 和
385'extern __inline__' 。
386
3874) 不要过度设计
388
389不要试图预计模糊的未来事情,这些事情也许有用也许没有用:"让事情尽可能的
390简单,而不是更简单"。
391
392----------------
393第三节 参考文献
394----------------
395
396Andrew Morton, "The perfect patch" (tpp).
397 <http://www.zip.com.au/~akpm/linux/patches/stuff/tpp.txt>
398
399Jeff Garzik, "Linux kernel patch submission format".
400 <http://linux.yyz.us/patch-format.html>
401
402Greg Kroah-Hartman, "How to piss off a kernel subsystem maintainer".
403 <http://www.kroah.com/log/2005/03/31/>
404 <http://www.kroah.com/log/2005/07/08/>
405 <http://www.kroah.com/log/2005/10/19/>
406 <http://www.kroah.com/log/2006/01/11/>
407
408NO!!!! No more huge patch bombs to linux-kernel@vger.kernel.org people!
409 <http://marc.theaimsgroup.com/?l=linux-kernel&m=112112749912944&w=2>
410
411Kernel Documentation/CodingStyle:
412 <http://sosdg.org/~coywolf/lxr/source/Documentation/CodingStyle>
413
414Linus Torvalds's mail on the canonical patch format:
415 <http://lkml.org/lkml/2005/4/7/183>
416--
diff --git a/Documentation/zh_CN/oops-tracing.txt b/Documentation/zh_CN/oops-tracing.txt
new file mode 100644
index 000000000000..9312608ffb8d
--- /dev/null
+++ b/Documentation/zh_CN/oops-tracing.txt
@@ -0,0 +1,212 @@
1Chinese translated version of Documentation/oops-tracing.txt
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Chinese maintainer: Dave Young <hidave.darkstar@gmail.com>
10---------------------------------------------------------------------
11Documentation/oops-tracing.txt 的中文翻译
12
13如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
14交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
15译存在问题,请联系中文版维护者。
16
17中文版维护者: 杨瑞 Dave Young <hidave.darkstar@gmail.com>
18中文版翻译者: 杨瑞 Dave Young <hidave.darkstar@gmail.com>
19中文版校译者: 李阳 Li Yang <leo@zh-kernel.org>
20 王聪 Wang Cong <xiyou.wangcong@gmail.com>
21
22以下为正文
23---------------------------------------------------------------------
24
25注意: ksymoops 在2.6中是没有用的。 请以原有格式使用Oops(来自dmesg,等等)。
26忽略任何这样那样关于“解码Oops”或者“通过ksymoops运行”的文档。 如果你贴出运行过
27ksymoops的来自2.6的Oops,人们只会让你重贴一次。
28
29快速总结
30-------------
31
32发现Oops并发送给看似相关的内核领域的维护者。别太担心对不上号。如果你不确定就发给
33和你所做的事情相关的代码的负责人。 如果可重现试着描述怎样重构。 那甚至比oops更有
34价值。
35
36如果你对于发送给谁一无所知, 发给linux-kernel@vger.kernel.org。感谢你帮助Linux
37尽可能地稳定。
38
39Oops在哪里?
40----------------------
41
42通常Oops文本由klogd从内核缓冲区里读取并传给syslogd,由syslogd写到syslog文件中,
43典型地是/var/log/messages(依赖于/etc/syslog.conf)。有时klogd崩溃了,这种情况下你
44能够运行dmesg > file来从内核缓冲区中读取数据并保存下来。 否则你可以
45cat /proc/kmsg > file, 然而你必须介入中止传输, kmsg是一个“永不结束的文件”。如
46果机器崩溃坏到你不能输入命令或者磁盘不可用那么你有三种选择:-
47
48(1) 手抄屏幕上的文本待机器重启后再输入计算机。 麻烦但如果没有针对崩溃的准备,
49这是仅有的选择。 另外,你可以用数码相机把屏幕拍下来-不太好,但比没有强。 如果信
50息滚动到了终端的上面,你会发现以高分辩率启动(比如,vga=791)会让你读到更多的文
51本。(注意:这需要vesafb,所以对‘早期’的oops没有帮助)
52
53(2)用串口终端启动(请参看Documentation/serial-console.txt),运行一个null
54modem到另一台机器并用你喜欢的通讯工具获取输出。Minicom工作地很好。
55
56(3)使用Kdump(请参看Documentation/kdump/kdump.txt),
57使用在Documentation/kdump/gdbmacros.txt中定义的dmesg gdb宏,从旧的内存中提取内核
58环形缓冲区。
59
60完整信息
61----------------
62
63注意:以下来自于Linus的邮件适用于2.4内核。 我因为历史原因保留了它,并且因为其中
64一些信息仍然适用。 特别注意的是,请忽略任何ksymoops的引用。
65
66From: Linus Torvalds <torvalds@osdl.org>
67
68怎样跟踪Oops.. [原发到linux-kernel的一封邮件]
69
70主要的窍门是有五年和这些烦人的oops消息打交道的经验;-)
71
72实际上,你有办法使它更简单。我有两个不同的方法:
73
74 gdb /usr/src/linux/vmlinux
75 gdb> disassemble <offending_function>
76
77那是发现问题的简单办法,至少如果bug报告做的好的情况下(象这个一样-运行ksymoops
78得到oops发生的函数及函数内的偏移)。
79
80哦,如果报告发生的内核以相同的编译器和相似的配置编译它会有帮助的。
81
82另一件要做的事是反汇编bug报告的“Code”部分:ksymoops也会用正确的工具来做这件事,
83但如果没有那些工具你可以写一个傻程序:
84
85 char str[] = "\xXX\xXX\xXX...";
86 main(){}
87
88并用gcc -g编译它然后执行“disassemble str”(XX部分是由Oops报告的值-你可以仅剪切
89粘贴并用“\x”替换空格-我就是这么做的,因为我懒得写程序自动做这一切)。
90
91另外,你可以用scripts/decodecode这个shell脚本。它的使用方法是:
92decodecode < oops.txt
93
94“Code”之后的十六进制字节可能(在某些架构上)有一些当前指令之前的指令字节以及
95当前和之后的指令字节
96
97Code: f9 0f 8d f9 00 00 00 8d 42 0c e8 dd 26 11 c7 a1 60 ea 2b f9 8b 50 08 a1
9864 ea 2b f9 8d 34 82 8b 1e 85 db 74 6d 8b 15 60 ea 2b f9 <8b> 43 04 39 42 54
997e 04 40 89 42 54 8b 43 04 3b 05 00 f6 52 c0
100
101最后,如果你想知道代码来自哪里,你可以:
102
103 cd /usr/src/linux
104 make fs/buffer.s # 或任何产生BUG的文件
105
106然后你会比gdb反汇编更清楚的知道发生了什么。
107
108现在,问题是把你所拥有的所有数据结合起来:C源码(关于它应该怎样的一般知识),
109汇编代码及其反汇编得到的代码(另外还有从“oops”消息得到的寄存器状态-对了解毁坏的
110指针有用,而且当你有了汇编代码你也能拿其它的寄存器和任何它们对应的C表达式做匹配
111)。
112
113实际上,你仅需看看哪里不匹配(这个例子是“Code”反汇编和编译器生成的代码不匹配)。
114然后你须要找出为什么不匹配。通常很简单-你看到代码使用了空指针然后你看代码想知道
115空指针是怎么出现的,还有检查它是否合法..
116
117现在,如果明白这是一项耗时的工作而且需要一丁点儿的专心,没错。这就是我为什么大多
118只是忽略那些没有符号表信息的崩溃报告的原因:简单的说太难查找了(我有一些
119程序用于在内核代码段中搜索特定的模式,而且有时我也已经能找出那些崩溃的地方,但是
120仅仅是找出正确的序列也确实需要相当扎实的内核知识)
121
122_有时_会发生这种情况,我仅看到崩溃中的反汇编代码序列, 然后我马上就明白问题出在
123哪里。这时我才意识到自己干这个工作已经太长时间了;-)
124
125 Linus
126
127
128---------------------------------------------------------------------------
129关于Oops跟踪的注解:
130
131为了帮助Linus和其它内核开发者,klogd纳入了大量的支持来处理保护错误。为了拥有对
132地址解析的完整支持至少应该使用1.3-pl3的sysklogd包。
133
134当保护错误发生时,klogd守护进程自动把内核日志信息中的重要地址翻译成它们相应的符
135号。
136
137klogd执行两种类型的地址解析。首先是静态翻译其次是动态翻译。静态翻译和ksymoops
138一样使用System.map文件。为了做静态翻译klogd守护进程必须在初始化时能找到system
139map文件。关于klogd怎样搜索map文件请参看klogd手册页。
140
141动态地址翻译在使用内核可装载模块时很重要。 因为内核模块的内存是从内核动态内存池
142里分配的,所以不管是模块开始位置还是模块中函数和符号的位置都不是固定的。
143
144内核支持允许程序决定装载哪些模块和它们在内存中位置的系统调用。使用这些系统调用
145klogd守护进程生成一张符号表用于调试发生在可装载模块中的保护错误。
146
147至少klogd会提供产生保护错误的模块名。还可有额外的符号信息供可装载模块开发者选择
148以从模块中输出符号信息。
149
150因为内核模块环境可能是动态的,所以必须有一种机制当模块环境发生改变时来通知klogd
151守护进程。 有一些可用的命令行选项允许klogd向当前执行中的守护进程发送信号,告知符
152号信息应该被刷新了。 更多信息请参看klogd手册页。
153
154sysklogd发布时包含一个补丁修改了modules-2.0.0包,无论何时一个模块装载或者卸载都
155会自动向klogd发送信号。打上这个补丁提供了必要的对调试发生于内核可装载模块的保护
156错误的无缝支持。
157
158以下是被klogd处理过的发生在可装载模块中的一个保护错误例子:
159---------------------------------------------------------------------------
160Aug 29 09:51:01 blizard kernel: Unable to handle kernel paging request at virtual address f15e97cc
161Aug 29 09:51:01 blizard kernel: current->tss.cr3 = 0062d000, %cr3 = 0062d000
162Aug 29 09:51:01 blizard kernel: *pde = 00000000
163Aug 29 09:51:01 blizard kernel: Oops: 0002
164Aug 29 09:51:01 blizard kernel: CPU: 0
165Aug 29 09:51:01 blizard kernel: EIP: 0010:[oops:_oops+16/3868]
166Aug 29 09:51:01 blizard kernel: EFLAGS: 00010212
167Aug 29 09:51:01 blizard kernel: eax: 315e97cc ebx: 003a6f80 ecx: 001be77b edx: 00237c0c
168Aug 29 09:51:01 blizard kernel: esi: 00000000 edi: bffffdb3 ebp: 00589f90 esp: 00589f8c
169Aug 29 09:51:01 blizard kernel: ds: 0018 es: 0018 fs: 002b gs: 002b ss: 0018
170Aug 29 09:51:01 blizard kernel: Process oops_test (pid: 3374, process nr: 21, stackpage=00589000)
171Aug 29 09:51:01 blizard kernel: Stack: 315e97cc 00589f98 0100b0b4 bffffed4 0012e38e 00240c64 003a6f80 00000001
172Aug 29 09:51:01 blizard kernel: 00000000 00237810 bfffff00 0010a7fa 00000003 00000001 00000000 bfffff00
173Aug 29 09:51:01 blizard kernel: bffffdb3 bffffed4 ffffffda 0000002b 0007002b 0000002b 0000002b 00000036
174Aug 29 09:51:01 blizard kernel: Call Trace: [oops:_oops_ioctl+48/80] [_sys_ioctl+254/272] [_system_call+82/128]
175Aug 29 09:51:01 blizard kernel: Code: c7 00 05 00 00 00 eb 08 90 90 90 90 90 90 90 90 89 ec 5d c3
176---------------------------------------------------------------------------
177
178Dr. G.W. Wettstein Oncology Research Div. Computing Facility
179Roger Maris Cancer Center INTERNET: greg@wind.rmcc.com
180820 4th St. N.
181Fargo, ND 58122
182Phone: 701-234-7556
183
184
185---------------------------------------------------------------------------
186受污染的内核
187
188一些oops报告在程序记数器之后包含字符串'Tainted: '。这表明内核已经被一些东西给污
189染了。 该字符串之后紧跟着一系列的位置敏感的字符,每个代表一个特定的污染值。
190
191 1:'G'如果所有装载的模块都有GPL或相容的许可证,'P'如果装载了任何的专有模块。
192没有模块MODULE_LICENSE或者带有insmod认为是与GPL不相容的的MODULE_LICENSE的模块被
193认定是专有的。
194
195 2:'F'如果有任何通过“insmod -f”被强制装载的模块,' '如果所有模块都被正常装载。
196
197 3:'S'如果oops发生在SMP内核中,运行于没有证明安全运行多处理器的硬件。 当前这种
198情况仅限于几种不支持SMP的速龙处理器。
199
200 4:'R'如果模块通过“insmod -f”被强制装载,' '如果所有模块都被正常装载。
201
202 5:'M'如果任何处理器报告了机器检查异常,' '如果没有发生机器检查异常。
203
204 6:'B'如果页释放函数发现了一个错误的页引用或者一些非预期的页标志。
205
206 7:'U'如果用户或者用户应用程序特别请求设置污染标志,否则' '。
207
208 8:'D'如果内核刚刚死掉,比如有OOPS或者BUG。
209
210使用'Tainted: '字符串的主要原因是要告诉内核调试者,这是否是一个干净的内核亦或发
211生了任何的不正常的事。污染是永久的:即使出错的模块已经被卸载了,污染值仍然存在,
212以表明内核不再值得信任。
diff --git a/Documentation/zh_CN/sparse.txt b/Documentation/zh_CN/sparse.txt
new file mode 100644
index 000000000000..75992a603ae3
--- /dev/null
+++ b/Documentation/zh_CN/sparse.txt
@@ -0,0 +1,100 @@
1Chinese translated version of Documentation/sparse.txt
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Chinese maintainer: Li Yang <leo@zh-kernel.org>
10---------------------------------------------------------------------
11Documentation/sparse.txt 的中文翻译
12
13如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
14交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
15译存在问题,请联系中文版维护者。
16
17中文版维护者: 李阳 Li Yang <leo@zh-kernel.org>
18中文版翻译者: 李阳 Li Yang <leo@zh-kernel.org>
19
20
21以下为正文
22---------------------------------------------------------------------
23
24Copyright 2004 Linus Torvalds
25Copyright 2004 Pavel Machek <pavel@suse.cz>
26Copyright 2006 Bob Copeland <me@bobcopeland.com>
27
28使用 sparse 工具做类型检查
29~~~~~~~~~~~~~~~~~~~~~~~~~~
30
31"__bitwise" 是一种类型属性,所以你应该这样使用它:
32
33 typedef int __bitwise pm_request_t;
34
35 enum pm_request {
36 PM_SUSPEND = (__force pm_request_t) 1,
37 PM_RESUME = (__force pm_request_t) 2
38 };
39
40这样会使 PM_SUSPEND 和 PM_RESUME 成为位方式(bitwise)整数(使用"__force"
41是因为 sparse 会抱怨改变位方式的类型转换,但是这里我们确实需要强制进行转
42换)。而且因为所有枚举值都使用了相同的类型,这里的"enum pm_request"也将
43会使用那个类型做为底层实现。
44
45而且使用 gcc 编译的时候,所有的 __bitwise/__force 都会消失,最后在 gcc
46看来它们只不过是普通的整数。
47
48坦白来说,你并不需要使用枚举类型。上面那些实际都可以浓缩成一个特殊的"int
49__bitwise"类型。
50
51所以更简单的办法只要这样做:
52
53 typedef int __bitwise pm_request_t;
54
55 #define PM_SUSPEND ((__force pm_request_t) 1)
56 #define PM_RESUME ((__force pm_request_t) 2)
57
58现在你就有了严格的类型检查所需要的所有基础架构。
59
60一个小提醒:常数整数"0"是特殊的。你可以直接把常数零当作位方式整数使用而
61不用担心 sparse 会抱怨。这是因为"bitwise"(恰如其名)是用来确保不同位方
62式类型不会被弄混(小尾模式,大尾模式,cpu尾模式,或者其他),对他们来说
63常数"0"确实是特殊的。
64
65获取 sparse 工具
66~~~~~~~~~~~~~~~~
67
68你可以从 Sparse 的主页获取最新的发布版本:
69
70 http://www.kernel.org/pub/linux/kernel/people/josh/sparse/
71
72或者,你也可以使用 git 克隆最新的 sparse 开发版本:
73
74 git://git.kernel.org/pub/scm/linux/kernel/git/josh/sparse.git
75
76DaveJ 把每小时自动生成的 git 源码树 tar 包放在以下地址:
77
78 http://www.codemonkey.org.uk/projects/git-snapshots/sparse/
79
80一旦你下载了源码,只要以普通用户身份运行:
81
82 make
83 make install
84
85它将会被自动安装到你的 ~/bin 目录下。
86
87使用 sparse 工具
88~~~~~~~~~~~~~~~~
89
90用"make C=1"命令来编译内核,会对所有重新编译的 C 文件使用 sparse 工具。
91或者使用"make C=2"命令,无论文件是否被重新编译都会对其使用 sparse 工具。
92如果你已经编译了内核,用后一种方式可以很快地检查整个源码树。
93
94make 的可选变量 CHECKFLAGS 可以用来向 sparse 工具传递参数。编译系统会自
95动向 sparse 工具传递 -Wbitwise 参数。你可以定义 __CHECK_ENDIAN__ 来进行
96大小尾检查。
97
98 make C=2 CHECKFLAGS="-D__CHECK_ENDIAN__"
99
100这些检查默认都是被关闭的,因为他们通常会产生大量的警告。
diff --git a/Documentation/zh_CN/stable_kernel_rules.txt b/Documentation/zh_CN/stable_kernel_rules.txt
new file mode 100644
index 000000000000..b5b9b0ab02fd
--- /dev/null
+++ b/Documentation/zh_CN/stable_kernel_rules.txt
@@ -0,0 +1,66 @@
1Chinese translated version of Documentation/stable_kernel_rules.txt
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Chinese maintainer: TripleX Chung <triplex@zh-kernel.org>
10---------------------------------------------------------------------
11Documentation/stable_kernel_rules.txt 的中文翻译
12
13如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
14交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
15译存在问题,请联系中文版维护者。
16
17
18中文版维护者: 钟宇 TripleX Chung <triplex@zh-kernel.org>
19中文版翻译者: 钟宇 TripleX Chung <triplex@zh-kernel.org>
20中文版校译者: 李阳 Li Yang <leo@zh-kernel.org>
21 Kangkai Yin <e12051@motorola.com>
22
23以下为正文
24---------------------------------------------------------------------
25
26关于Linux 2.6稳定版发布,所有你想知道的事情。
27
28关于哪些类型的补丁可以被接收进入稳定版代码树,哪些不可以的规则:
29
30 - 必须是显而易见的正确,并且经过测试的。
31 - 连同上下文,不能大于100行。
32 - 必须只修正一件事情。
33 - 必须修正了一个给大家带来麻烦的真正的bug(不是“这也许是一个问题...”
34 那样的东西)。
35 - 必须修正带来如下后果的问题:编译错误(对被标记为CONFIG_BROKEN的例外),
36 内核崩溃,挂起,数据损坏,真正的安全问题,或者一些类似“哦,这不
37 好”的问题。简短的说,就是一些致命的问题。
38 - 没有“理论上的竞争条件”,除非能给出竞争条件如何被利用的解释。
39 - 不能存在任何的“琐碎的”修正(拼写修正,去掉多余空格之类的)。
40 - 必须被相关子系统的维护者接受。
41 - 必须遵循Documentation/SubmittingPatches里的规则。
42
43向稳定版代码树提交补丁的过程:
44
45 - 在确认了补丁符合以上的规则后,将补丁发送到stable@kernel.org。
46 - 如果补丁被接受到队列里,发送者会收到一个ACK回复,如果没有被接受,收
47 到的是NAK回复。回复需要几天的时间,这取决于开发者的时间安排。
48 - 被接受的补丁会被加到稳定版本队列里,等待其他开发者的审查。
49 - 安全方面的补丁不要发到这个列表,应该发送到security@kernel.org。
50
51审查周期:
52
53 - 当稳定版的维护者决定开始一个审查周期,补丁将被发送到审查委员会,以
54 及被补丁影响的领域的维护者(除非提交者就是该领域的维护者)并且抄送
55 到linux-kernel邮件列表。
56 - 审查委员会有48小时的时间,用来决定给该补丁回复ACK还是NAK。
57 - 如果委员会中有成员拒绝这个补丁,或者linux-kernel列表上有人反对这个
58 补丁,并提出维护者和审查委员会之前没有意识到的问题,补丁会从队列中
59 丢弃。
60 - 在审查周期结束的时候,那些得到ACK回应的补丁将会被加入到最新的稳定版
61 发布中,一个新的稳定版发布就此产生。
62 - 安全性补丁将从内核安全小组那里直接接收到稳定版代码树中,而不是通过
63 通常的审查周期。请联系内核安全小组以获得关于这个过程的更多细节。
64
65审查委员会:
66 - 由一些自愿承担这项任务的内核开发者,和几个非志愿的组成。
diff --git a/Documentation/zh_CN/volatile-considered-harmful.txt b/Documentation/zh_CN/volatile-considered-harmful.txt
new file mode 100644
index 000000000000..ba8149d2233a
--- /dev/null
+++ b/Documentation/zh_CN/volatile-considered-harmful.txt
@@ -0,0 +1,113 @@
1Chinese translated version of Documentation/volatile-considered-harmful.txt
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Maintainer: Jonathan Corbet <corbet@lwn.net>
10Chinese maintainer: Bryan Wu <bryan.wu@analog.com>
11---------------------------------------------------------------------
12Documentation/volatile-considered-harmful.txt 的中文翻译
13
14如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
15交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
16译存在问题,请联系中文版维护者。
17
18英文版维护者: Jonathan Corbet <corbet@lwn.net>
19中文版维护者: 伍鹏 Bryan Wu <bryan.wu@analog.com>
20中文版翻译者: 伍鹏 Bryan Wu <bryan.wu@analog.com>
21中文版校译者: 张汉辉 Eugene Teo <eugeneteo@kernel.sg>
22 杨瑞 Dave Young <hidave.darkstar@gmail.com>
23以下为正文
24---------------------------------------------------------------------
25
26为什么不应该使用“volatile”类型
27------------------------------
28
29C程序员通常认为volatile表示某个变量可以在当前执行的线程之外被改变;因此,在内核
30中用到共享数据结构时,常常会有C程序员喜欢使用volatile这类变量。换句话说,他们经
31常会把volatile类型看成某种简易的原子变量,当然它们不是。在内核中使用volatile几
32乎总是错误的;本文档将解释为什么这样。
33
34理解volatile的关键是知道它的目的是用来消除优化,实际上很少有人真正需要这样的应
35用。在内核中,程序员必须防止意外的并发访问破坏共享的数据结构,这其实是一个完全
36不同的任务。用来防止意外并发访问的保护措施,可以更加高效的避免大多数优化相关的
37问题。
38
39像volatile一样,内核提供了很多原语来保证并发访问时的数据安全(自旋锁, 互斥量,内
40存屏障等等),同样可以防止意外的优化。如果可以正确使用这些内核原语,那么就没有
41必要再使用volatile。如果仍然必须使用volatile,那么几乎可以肯定在代码的某处有一
42个bug。在正确设计的内核代码中,volatile能带来的仅仅是使事情变慢。
43
44思考一下这段典型的内核代码:
45
46 spin_lock(&the_lock);
47 do_something_on(&shared_data);
48 do_something_else_with(&shared_data);
49 spin_unlock(&the_lock);
50
51如果所有的代码都遵循加锁规则,当持有the_lock的时候,不可能意外的改变shared_data的
52值。任何可能访问该数据的其他代码都会在这个锁上等待。自旋锁原语跟内存屏障一样—— 它
53们显式的用来书写成这样 —— 意味着数据访问不会跨越它们而被优化。所以本来编译器认为
54它知道在shared_data里面将有什么,但是因为spin_lock()调用跟内存屏障一样,会强制编
55译器忘记它所知道的一切。那么在访问这些数据时不会有优化的问题。
56
57如果shared_data被声名为volatile,锁操作将仍然是必须的。就算我们知道没有其他人正在
58使用它,编译器也将被阻止优化对临界区内shared_data的访问。在锁有效的同时,
59shared_data不是volatile的。在处理共享数据的时候,适当的锁操作可以不再需要
60volatile —— 并且是有潜在危害的。
61
62volatile的存储类型最初是为那些内存映射的I/O寄存器而定义。在内核里,寄存器访问也应
63该被锁保护,但是人们也不希望编译器“优化”临界区内的寄存器访问。内核里I/O的内存访问
64是通过访问函数完成的;不赞成通过指针对I/O内存的直接访问,并且不是在所有体系架构上
65都能工作。那些访问函数正是为了防止意外优化而写的,因此,再说一次,volatile类型不
66是必需的。
67
68另一种引起用户可能使用volatile的情况是当处理器正忙着等待一个变量的值。正确执行一
69个忙等待的方法是:
70
71 while (my_variable != what_i_want)
72 cpu_relax();
73
74cpu_relax()调用会降低CPU的能量消耗或者让位于超线程双处理器;它也作为内存屏障一样出
75现,所以,再一次,volatile不是必需的。当然,忙等待一开始就是一种反常规的做法。
76
77在内核中,一些稀少的情况下volatile仍然是有意义的:
78
79 - 在一些体系架构的系统上,允许直接的I/0内存访问,那么前面提到的访问函数可以使用
80 volatile。基本上,每一个访问函数调用它自己都是一个小的临界区域并且保证了按照
81 程序员期望的那样发生访问操作。
82
83 - 某些会改变内存的内联汇编代码虽然没有什么其他明显的附作用,但是有被GCC删除的可
84 能性。在汇编声明中加上volatile关键字可以防止这种删除操作。
85
86 - Jiffies变量是一种特殊情况,虽然每次引用它的时候都可以有不同的值,但读jiffies
87 变量时不需要任何特殊的加锁保护。所以jiffies变量可以使用volatile,但是不赞成
88 其他跟jiffies相同类型变量使用volatile。Jiffies被认为是一种“愚蠢的遗留物"
89 (Linus的话)因为解决这个问题比保持现状要麻烦的多。
90
91 - 由于某些I/0设备可能会修改连续一致的内存,所以有时,指向连续一致内存的数据结构
92 的指针需要正确的使用volatile。网络适配器使用的环状缓存区正是这类情形的一个例
93 子,其中适配器用改变指针来表示哪些描述符已经处理过了。
94
95对于大多代码,上述几种可以使用volatile的情况都不适用。所以,使用volatile是一种
96bug并且需要对这样的代码额外仔细检查。那些试图使用volatile的开发人员需要退一步想想
97他们真正想实现的是什么。
98
99非常欢迎删除volatile变量的补丁 - 只要证明这些补丁完整的考虑了并发问题。
100
101注释
102----
103
104[1] http://lwn.net/Articles/233481/
105[2] http://lwn.net/Articles/233482/
106
107致谢
108----
109
110最初由Randy Dunlap推动并作初步研究
111由Jonathan Corbet撰写
112参考Satyam Sharma,Johannes Stezenbach,Jesper Juhl,Heikki Orsila,
113H. Peter Anvin,Philipp Hahn和Stefan Richter的意见改善了本档。
diff --git a/arch/arm/kernel/time.c b/arch/arm/kernel/time.c
index 1533d3ecd7a0..f6f3689a86ee 100644
--- a/arch/arm/kernel/time.c
+++ b/arch/arm/kernel/time.c
@@ -195,7 +195,7 @@ static int leds_shutdown(struct sys_device *dev)
195} 195}
196 196
197static struct sysdev_class leds_sysclass = { 197static struct sysdev_class leds_sysclass = {
198 set_kset_name("leds"), 198 .name = "leds",
199 .shutdown = leds_shutdown, 199 .shutdown = leds_shutdown,
200 .suspend = leds_suspend, 200 .suspend = leds_suspend,
201 .resume = leds_resume, 201 .resume = leds_resume,
@@ -369,7 +369,7 @@ static int timer_resume(struct sys_device *dev)
369#endif 369#endif
370 370
371static struct sysdev_class timer_sysclass = { 371static struct sysdev_class timer_sysclass = {
372 set_kset_name("timer"), 372 .name = "timer",
373 .suspend = timer_suspend, 373 .suspend = timer_suspend,
374 .resume = timer_resume, 374 .resume = timer_resume,
375}; 375};
diff --git a/arch/arm/mach-integrator/integrator_ap.c b/arch/arm/mach-integrator/integrator_ap.c
index 72280754354d..df37e93c6fc9 100644
--- a/arch/arm/mach-integrator/integrator_ap.c
+++ b/arch/arm/mach-integrator/integrator_ap.c
@@ -214,7 +214,7 @@ static int irq_resume(struct sys_device *dev)
214#endif 214#endif
215 215
216static struct sysdev_class irq_class = { 216static struct sysdev_class irq_class = {
217 set_kset_name("irq"), 217 .name = "irq",
218 .suspend = irq_suspend, 218 .suspend = irq_suspend,
219 .resume = irq_resume, 219 .resume = irq_resume,
220}; 220};
diff --git a/arch/arm/mach-omap1/pm.c b/arch/arm/mach-omap1/pm.c
index 3bf01e28df33..d9805e3d9304 100644
--- a/arch/arm/mach-omap1/pm.c
+++ b/arch/arm/mach-omap1/pm.c
@@ -69,14 +69,14 @@ static unsigned int mpui1610_sleep_save[MPUI1610_SLEEP_SAVE_SIZE];
69 69
70static unsigned short enable_dyn_sleep = 1; 70static unsigned short enable_dyn_sleep = 1;
71 71
72static ssize_t omap_pm_sleep_while_idle_show(struct kset *kset, char *buf) 72static ssize_t idle_show(struct kobject *kobj, struct kobj_attribute *attr,
73 char *buf)
73{ 74{
74 return sprintf(buf, "%hu\n", enable_dyn_sleep); 75 return sprintf(buf, "%hu\n", enable_dyn_sleep);
75} 76}
76 77
77static ssize_t omap_pm_sleep_while_idle_store(struct kset *kset, 78static ssize_t idle_store(struct kobject *kobj, struct kobj_attribute *attr,
78 const char * buf, 79 const char * buf, size_t n)
79 size_t n)
80{ 80{
81 unsigned short value; 81 unsigned short value;
82 if (sscanf(buf, "%hu", &value) != 1 || 82 if (sscanf(buf, "%hu", &value) != 1 ||
@@ -88,16 +88,9 @@ static ssize_t omap_pm_sleep_while_idle_store(struct kset *kset,
88 return n; 88 return n;
89} 89}
90 90
91static struct subsys_attribute sleep_while_idle_attr = { 91static struct kobj_attribute sleep_while_idle_attr =
92 .attr = { 92 __ATTR(sleep_while_idle, 0644, idle_show, idle_store);
93 .name = __stringify(sleep_while_idle),
94 .mode = 0644,
95 },
96 .show = omap_pm_sleep_while_idle_show,
97 .store = omap_pm_sleep_while_idle_store,
98};
99 93
100extern struct kset power_subsys;
101static void (*omap_sram_idle)(void) = NULL; 94static void (*omap_sram_idle)(void) = NULL;
102static void (*omap_sram_suspend)(unsigned long r0, unsigned long r1) = NULL; 95static void (*omap_sram_suspend)(unsigned long r0, unsigned long r1) = NULL;
103 96
@@ -726,9 +719,9 @@ static int __init omap_pm_init(void)
726 omap_pm_init_proc(); 719 omap_pm_init_proc();
727#endif 720#endif
728 721
729 error = subsys_create_file(&power_subsys, &sleep_while_idle_attr); 722 error = sysfs_create_file(power_kobj, &sleep_while_idle_attr);
730 if (error) 723 if (error)
731 printk(KERN_ERR "subsys_create_file failed: %d\n", error); 724 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
732 725
733 if (cpu_is_omap16xx()) { 726 if (cpu_is_omap16xx()) {
734 /* configure LOW_PWR pin */ 727 /* configure LOW_PWR pin */
diff --git a/arch/arm/mach-pxa/cm-x270.c b/arch/arm/mach-pxa/cm-x270.c
index 177664ccb2e2..a16349272f54 100644
--- a/arch/arm/mach-pxa/cm-x270.c
+++ b/arch/arm/mach-pxa/cm-x270.c
@@ -566,7 +566,7 @@ static int cmx270_resume(struct sys_device *dev)
566} 566}
567 567
568static struct sysdev_class cmx270_pm_sysclass = { 568static struct sysdev_class cmx270_pm_sysclass = {
569 set_kset_name("pm"), 569 .name = "pm",
570 .resume = cmx270_resume, 570 .resume = cmx270_resume,
571 .suspend = cmx270_suspend, 571 .suspend = cmx270_suspend,
572}; 572};
diff --git a/arch/arm/mach-pxa/lpd270.c b/arch/arm/mach-pxa/lpd270.c
index 26116440a7c9..78ebad063cba 100644
--- a/arch/arm/mach-pxa/lpd270.c
+++ b/arch/arm/mach-pxa/lpd270.c
@@ -122,7 +122,7 @@ static int lpd270_irq_resume(struct sys_device *dev)
122} 122}
123 123
124static struct sysdev_class lpd270_irq_sysclass = { 124static struct sysdev_class lpd270_irq_sysclass = {
125 set_kset_name("cpld_irq"), 125 .name = "cpld_irq",
126 .resume = lpd270_irq_resume, 126 .resume = lpd270_irq_resume,
127}; 127};
128 128
diff --git a/arch/arm/mach-pxa/lubbock.c b/arch/arm/mach-pxa/lubbock.c
index 011a1a72b61c..1d3112dc629e 100644
--- a/arch/arm/mach-pxa/lubbock.c
+++ b/arch/arm/mach-pxa/lubbock.c
@@ -126,7 +126,7 @@ static int lubbock_irq_resume(struct sys_device *dev)
126} 126}
127 127
128static struct sysdev_class lubbock_irq_sysclass = { 128static struct sysdev_class lubbock_irq_sysclass = {
129 set_kset_name("cpld_irq"), 129 .name = "cpld_irq",
130 .resume = lubbock_irq_resume, 130 .resume = lubbock_irq_resume,
131}; 131};
132 132
diff --git a/arch/arm/mach-pxa/mainstone.c b/arch/arm/mach-pxa/mainstone.c
index a4bc3483cbb3..41d8c6cea62b 100644
--- a/arch/arm/mach-pxa/mainstone.c
+++ b/arch/arm/mach-pxa/mainstone.c
@@ -120,7 +120,7 @@ static int mainstone_irq_resume(struct sys_device *dev)
120} 120}
121 121
122static struct sysdev_class mainstone_irq_sysclass = { 122static struct sysdev_class mainstone_irq_sysclass = {
123 set_kset_name("cpld_irq"), 123 .name = "cpld_irq",
124 .resume = mainstone_irq_resume, 124 .resume = mainstone_irq_resume,
125}; 125};
126 126
diff --git a/arch/arm/mach-s3c2410/s3c2410.c b/arch/arm/mach-s3c2410/s3c2410.c
index e580303cb0ab..0e7991940f81 100644
--- a/arch/arm/mach-s3c2410/s3c2410.c
+++ b/arch/arm/mach-s3c2410/s3c2410.c
@@ -100,7 +100,7 @@ void __init s3c2410_init_clocks(int xtal)
100} 100}
101 101
102struct sysdev_class s3c2410_sysclass = { 102struct sysdev_class s3c2410_sysclass = {
103 set_kset_name("s3c2410-core"), 103 .name = "s3c2410-core",
104}; 104};
105 105
106static struct sys_device s3c2410_sysdev = { 106static struct sys_device s3c2410_sysdev = {
diff --git a/arch/arm/mach-s3c2412/s3c2412.c b/arch/arm/mach-s3c2412/s3c2412.c
index 4f92a1562d77..265cd3f567a3 100644
--- a/arch/arm/mach-s3c2412/s3c2412.c
+++ b/arch/arm/mach-s3c2412/s3c2412.c
@@ -196,7 +196,7 @@ void __init s3c2412_init_clocks(int xtal)
196*/ 196*/
197 197
198struct sysdev_class s3c2412_sysclass = { 198struct sysdev_class s3c2412_sysclass = {
199 set_kset_name("s3c2412-core"), 199 .name = "s3c2412-core",
200}; 200};
201 201
202static int __init s3c2412_core_init(void) 202static int __init s3c2412_core_init(void)
diff --git a/arch/arm/mach-s3c2440/mach-osiris.c b/arch/arm/mach-s3c2440/mach-osiris.c
index c326983f4a8f..78af7664988b 100644
--- a/arch/arm/mach-s3c2440/mach-osiris.c
+++ b/arch/arm/mach-s3c2440/mach-osiris.c
@@ -312,7 +312,7 @@ static int osiris_pm_resume(struct sys_device *sd)
312#endif 312#endif
313 313
314static struct sysdev_class osiris_pm_sysclass = { 314static struct sysdev_class osiris_pm_sysclass = {
315 set_kset_name("mach-osiris"), 315 .name = "mach-osiris",
316 .suspend = osiris_pm_suspend, 316 .suspend = osiris_pm_suspend,
317 .resume = osiris_pm_resume, 317 .resume = osiris_pm_resume,
318}; 318};
diff --git a/arch/arm/mach-s3c2443/s3c2443.c b/arch/arm/mach-s3c2443/s3c2443.c
index 8d8117158d23..9ce490560af9 100644
--- a/arch/arm/mach-s3c2443/s3c2443.c
+++ b/arch/arm/mach-s3c2443/s3c2443.c
@@ -43,7 +43,7 @@ static struct map_desc s3c2443_iodesc[] __initdata = {
43}; 43};
44 44
45struct sysdev_class s3c2443_sysclass = { 45struct sysdev_class s3c2443_sysclass = {
46 set_kset_name("s3c2443-core"), 46 .name = "s3c2443-core",
47}; 47};
48 48
49static struct sys_device s3c2443_sysdev = { 49static struct sys_device s3c2443_sysdev = {
diff --git a/arch/arm/mach-sa1100/irq.c b/arch/arm/mach-sa1100/irq.c
index edf3347d9c5b..3dc17d7bf38e 100644
--- a/arch/arm/mach-sa1100/irq.c
+++ b/arch/arm/mach-sa1100/irq.c
@@ -283,7 +283,7 @@ static int sa1100irq_resume(struct sys_device *dev)
283} 283}
284 284
285static struct sysdev_class sa1100irq_sysclass = { 285static struct sysdev_class sa1100irq_sysclass = {
286 set_kset_name("sa11x0-irq"), 286 .name = "sa11x0-irq",
287 .suspend = sa1100irq_suspend, 287 .suspend = sa1100irq_suspend,
288 .resume = sa1100irq_resume, 288 .resume = sa1100irq_resume,
289}; 289};
diff --git a/arch/arm/oprofile/common.c b/arch/arm/oprofile/common.c
index a9de727c9327..0a5cf3a6438b 100644
--- a/arch/arm/oprofile/common.c
+++ b/arch/arm/oprofile/common.c
@@ -96,7 +96,7 @@ static int op_arm_resume(struct sys_device *dev)
96} 96}
97 97
98static struct sysdev_class oprofile_sysclass = { 98static struct sysdev_class oprofile_sysclass = {
99 set_kset_name("oprofile"), 99 .name = "oprofile",
100 .resume = op_arm_resume, 100 .resume = op_arm_resume,
101 .suspend = op_arm_suspend, 101 .suspend = op_arm_suspend,
102}; 102};
diff --git a/arch/arm/plat-omap/gpio.c b/arch/arm/plat-omap/gpio.c
index 6097753394ad..b2a87b8ef673 100644
--- a/arch/arm/plat-omap/gpio.c
+++ b/arch/arm/plat-omap/gpio.c
@@ -1455,7 +1455,7 @@ static int omap_gpio_resume(struct sys_device *dev)
1455} 1455}
1456 1456
1457static struct sysdev_class omap_gpio_sysclass = { 1457static struct sysdev_class omap_gpio_sysclass = {
1458 set_kset_name("gpio"), 1458 .name = "gpio",
1459 .suspend = omap_gpio_suspend, 1459 .suspend = omap_gpio_suspend,
1460 .resume = omap_gpio_resume, 1460 .resume = omap_gpio_resume,
1461}; 1461};
diff --git a/arch/arm/plat-s3c24xx/dma.c b/arch/arm/plat-s3c24xx/dma.c
index 29696e46ed65..aae1b9cbaf44 100644
--- a/arch/arm/plat-s3c24xx/dma.c
+++ b/arch/arm/plat-s3c24xx/dma.c
@@ -1265,7 +1265,7 @@ static int s3c2410_dma_resume(struct sys_device *dev)
1265#endif /* CONFIG_PM */ 1265#endif /* CONFIG_PM */
1266 1266
1267struct sysdev_class dma_sysclass = { 1267struct sysdev_class dma_sysclass = {
1268 set_kset_name("s3c24xx-dma"), 1268 .name = "s3c24xx-dma",
1269 .suspend = s3c2410_dma_suspend, 1269 .suspend = s3c2410_dma_suspend,
1270 .resume = s3c2410_dma_resume, 1270 .resume = s3c2410_dma_resume,
1271}; 1271};
diff --git a/arch/arm/plat-s3c24xx/s3c244x.c b/arch/arm/plat-s3c24xx/s3c244x.c
index 3444b13afac5..f197bb3a2366 100644
--- a/arch/arm/plat-s3c24xx/s3c244x.c
+++ b/arch/arm/plat-s3c24xx/s3c244x.c
@@ -151,13 +151,13 @@ static int s3c244x_resume(struct sys_device *dev)
151/* Since the S3C2442 and S3C2440 share items, put both sysclasses here */ 151/* Since the S3C2442 and S3C2440 share items, put both sysclasses here */
152 152
153struct sysdev_class s3c2440_sysclass = { 153struct sysdev_class s3c2440_sysclass = {
154 set_kset_name("s3c2440-core"), 154 .name = "s3c2440-core",
155 .suspend = s3c244x_suspend, 155 .suspend = s3c244x_suspend,
156 .resume = s3c244x_resume 156 .resume = s3c244x_resume
157}; 157};
158 158
159struct sysdev_class s3c2442_sysclass = { 159struct sysdev_class s3c2442_sysclass = {
160 set_kset_name("s3c2442-core"), 160 .name = "s3c2442-core",
161 .suspend = s3c244x_suspend, 161 .suspend = s3c244x_suspend,
162 .resume = s3c244x_resume 162 .resume = s3c244x_resume
163}; 163};
diff --git a/arch/avr32/kernel/time.c b/arch/avr32/kernel/time.c
index 7014a3571ec0..36a46c3ae308 100644
--- a/arch/avr32/kernel/time.c
+++ b/arch/avr32/kernel/time.c
@@ -214,7 +214,7 @@ void __init time_init(void)
214} 214}
215 215
216static struct sysdev_class timer_class = { 216static struct sysdev_class timer_class = {
217 set_kset_name("timer"), 217 .name = "timer",
218}; 218};
219 219
220static struct sys_device timer_device = { 220static struct sys_device timer_device = {
diff --git a/arch/cris/arch-v32/drivers/iop_fw_load.c b/arch/cris/arch-v32/drivers/iop_fw_load.c
index 11f9895ded50..f4bdc1dfa320 100644
--- a/arch/cris/arch-v32/drivers/iop_fw_load.c
+++ b/arch/cris/arch-v32/drivers/iop_fw_load.c
@@ -20,6 +20,9 @@
20 20
21#define IOP_TIMEOUT 100 21#define IOP_TIMEOUT 100
22 22
23#error "This driver is broken with regard to its driver core usage."
24#error "Please contact <greg@kroah.com> for details on how to fix it properly."
25
23static struct device iop_spu_device[2] = { 26static struct device iop_spu_device[2] = {
24 { .bus_id = "iop-spu0", }, 27 { .bus_id = "iop-spu0", },
25 { .bus_id = "iop-spu1", }, 28 { .bus_id = "iop-spu1", },
@@ -192,6 +195,13 @@ int iop_start_mpu(unsigned int start_addr)
192 195
193static int __init iop_fw_load_init(void) 196static int __init iop_fw_load_init(void)
194{ 197{
198#if 0
199 /*
200 * static struct devices can not be added directly to sysfs by ignoring
201 * the driver model infrastructure. To fix this properly, please use
202 * the platform_bus to register these devices to be able to properly
203 * use the firmware infrastructure.
204 */
195 device_initialize(&iop_spu_device[0]); 205 device_initialize(&iop_spu_device[0]);
196 kobject_set_name(&iop_spu_device[0].kobj, "iop-spu0"); 206 kobject_set_name(&iop_spu_device[0].kobj, "iop-spu0");
197 kobject_add(&iop_spu_device[0].kobj); 207 kobject_add(&iop_spu_device[0].kobj);
@@ -201,6 +211,7 @@ static int __init iop_fw_load_init(void)
201 device_initialize(&iop_mpu_device); 211 device_initialize(&iop_mpu_device);
202 kobject_set_name(&iop_mpu_device.kobj, "iop-mpu"); 212 kobject_set_name(&iop_mpu_device.kobj, "iop-mpu");
203 kobject_add(&iop_mpu_device.kobj); 213 kobject_add(&iop_mpu_device.kobj);
214#endif
204 return 0; 215 return 0;
205} 216}
206 217
diff --git a/arch/ia64/kernel/topology.c b/arch/ia64/kernel/topology.c
index 14261fee5f4d..a2484fc1a06c 100644
--- a/arch/ia64/kernel/topology.c
+++ b/arch/ia64/kernel/topology.c
@@ -354,27 +354,27 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
354 if (unlikely(retval < 0)) 354 if (unlikely(retval < 0))
355 return retval; 355 return retval;
356 356
357 all_cpu_cache_info[cpu].kobj.parent = &sys_dev->kobj; 357 retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
358 kobject_set_name(&all_cpu_cache_info[cpu].kobj, "%s", "cache"); 358 &cache_ktype_percpu_entry, &sys_dev->kobj,
359 all_cpu_cache_info[cpu].kobj.ktype = &cache_ktype_percpu_entry; 359 "%s", "cache");
360 retval = kobject_register(&all_cpu_cache_info[cpu].kobj);
361 360
362 for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) { 361 for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) {
363 this_object = LEAF_KOBJECT_PTR(cpu,i); 362 this_object = LEAF_KOBJECT_PTR(cpu,i);
364 this_object->kobj.parent = &all_cpu_cache_info[cpu].kobj; 363 retval = kobject_init_and_add(&(this_object->kobj),
365 kobject_set_name(&(this_object->kobj), "index%1lu", i); 364 &cache_ktype,
366 this_object->kobj.ktype = &cache_ktype; 365 &all_cpu_cache_info[cpu].kobj,
367 retval = kobject_register(&(this_object->kobj)); 366 "index%1lu", i);
368 if (unlikely(retval)) { 367 if (unlikely(retval)) {
369 for (j = 0; j < i; j++) { 368 for (j = 0; j < i; j++) {
370 kobject_unregister( 369 kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
371 &(LEAF_KOBJECT_PTR(cpu,j)->kobj));
372 } 370 }
373 kobject_unregister(&all_cpu_cache_info[cpu].kobj); 371 kobject_put(&all_cpu_cache_info[cpu].kobj);
374 cpu_cache_sysfs_exit(cpu); 372 cpu_cache_sysfs_exit(cpu);
375 break; 373 break;
376 } 374 }
375 kobject_uevent(&(this_object->kobj), KOBJ_ADD);
377 } 376 }
377 kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
378 return retval; 378 return retval;
379} 379}
380 380
@@ -385,10 +385,10 @@ static int __cpuinit cache_remove_dev(struct sys_device * sys_dev)
385 unsigned long i; 385 unsigned long i;
386 386
387 for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) 387 for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++)
388 kobject_unregister(&(LEAF_KOBJECT_PTR(cpu,i)->kobj)); 388 kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
389 389
390 if (all_cpu_cache_info[cpu].kobj.parent) { 390 if (all_cpu_cache_info[cpu].kobj.parent) {
391 kobject_unregister(&all_cpu_cache_info[cpu].kobj); 391 kobject_put(&all_cpu_cache_info[cpu].kobj);
392 memset(&all_cpu_cache_info[cpu].kobj, 392 memset(&all_cpu_cache_info[cpu].kobj,
393 0, 393 0,
394 sizeof(struct kobject)); 394 sizeof(struct kobject));
diff --git a/arch/mips/kernel/i8259.c b/arch/mips/kernel/i8259.c
index 471013577108..197d7977de35 100644
--- a/arch/mips/kernel/i8259.c
+++ b/arch/mips/kernel/i8259.c
@@ -238,7 +238,7 @@ static int i8259A_shutdown(struct sys_device *dev)
238} 238}
239 239
240static struct sysdev_class i8259_sysdev_class = { 240static struct sysdev_class i8259_sysdev_class = {
241 set_kset_name("i8259"), 241 .name = "i8259",
242 .resume = i8259A_resume, 242 .resume = i8259A_resume,
243 .shutdown = i8259A_shutdown, 243 .shutdown = i8259A_shutdown,
244}; 244};
diff --git a/arch/powerpc/platforms/cell/spu_base.c b/arch/powerpc/platforms/cell/spu_base.c
index c83c3e3f5178..a08862203643 100644
--- a/arch/powerpc/platforms/cell/spu_base.c
+++ b/arch/powerpc/platforms/cell/spu_base.c
@@ -459,7 +459,7 @@ static int spu_shutdown(struct sys_device *sysdev)
459} 459}
460 460
461static struct sysdev_class spu_sysdev_class = { 461static struct sysdev_class spu_sysdev_class = {
462 set_kset_name("spu"), 462 .name = "spu",
463 .shutdown = spu_shutdown, 463 .shutdown = spu_shutdown,
464}; 464};
465 465
diff --git a/arch/powerpc/platforms/powermac/pic.c b/arch/powerpc/platforms/powermac/pic.c
index 999f5e160897..84c0d4ef76a2 100644
--- a/arch/powerpc/platforms/powermac/pic.c
+++ b/arch/powerpc/platforms/powermac/pic.c
@@ -663,7 +663,7 @@ static int pmacpic_resume(struct sys_device *sysdev)
663#endif /* CONFIG_PM && CONFIG_PPC32 */ 663#endif /* CONFIG_PM && CONFIG_PPC32 */
664 664
665static struct sysdev_class pmacpic_sysclass = { 665static struct sysdev_class pmacpic_sysclass = {
666 set_kset_name("pmac_pic"), 666 .name = "pmac_pic",
667}; 667};
668 668
669static struct sys_device device_pmacpic = { 669static struct sys_device device_pmacpic = {
diff --git a/arch/powerpc/platforms/pseries/power.c b/arch/powerpc/platforms/pseries/power.c
index 73e69023d90a..e95fc1594c84 100644
--- a/arch/powerpc/platforms/pseries/power.c
+++ b/arch/powerpc/platforms/pseries/power.c
@@ -28,13 +28,15 @@
28 28
29unsigned long rtas_poweron_auto; /* default and normal state is 0 */ 29unsigned long rtas_poweron_auto; /* default and normal state is 0 */
30 30
31static ssize_t auto_poweron_show(struct kset *kset, char *buf) 31static ssize_t auto_poweron_show(struct kobject *kobj,
32 struct kobj_attribute *attr, char *buf)
32{ 33{
33 return sprintf(buf, "%lu\n", rtas_poweron_auto); 34 return sprintf(buf, "%lu\n", rtas_poweron_auto);
34} 35}
35 36
36static ssize_t 37static ssize_t auto_poweron_store(struct kobject *kobj,
37auto_poweron_store(struct kset *kset, const char *buf, size_t n) 38 struct kobj_attribute *attr,
39 const char *buf, size_t n)
38{ 40{
39 int ret; 41 int ret;
40 unsigned long ups_restart; 42 unsigned long ups_restart;
@@ -47,17 +49,11 @@ auto_poweron_store(struct kset *kset, const char *buf, size_t n)
47 return -EINVAL; 49 return -EINVAL;
48} 50}
49 51
50static struct subsys_attribute auto_poweron_attr = { 52static struct kobj_attribute auto_poweron_attr =
51 .attr = { 53 __ATTR(auto_poweron, 0644, auto_poweron_show, auto_poweron_store);
52 .name = __stringify(auto_poweron),
53 .mode = 0644,
54 },
55 .show = auto_poweron_show,
56 .store = auto_poweron_store,
57};
58 54
59#ifndef CONFIG_PM 55#ifndef CONFIG_PM
60decl_subsys(power,NULL,NULL); 56struct kobject *power_kobj;
61 57
62static struct attribute *g[] = { 58static struct attribute *g[] = {
63 &auto_poweron_attr.attr, 59 &auto_poweron_attr.attr,
@@ -70,18 +66,16 @@ static struct attribute_group attr_group = {
70 66
71static int __init pm_init(void) 67static int __init pm_init(void)
72{ 68{
73 int error = subsystem_register(&power_subsys); 69 power_kobj = kobject_create_and_add("power", NULL);
74 if (!error) 70 if (!power_kobj)
75 error = sysfs_create_group(&power_subsys.kobj, &attr_group); 71 return -ENOMEM;
76 return error; 72 return sysfs_create_group(power_kobj, &attr_group);
77} 73}
78core_initcall(pm_init); 74core_initcall(pm_init);
79#else 75#else
80extern struct kset power_subsys;
81
82static int __init apo_pm_init(void) 76static int __init apo_pm_init(void)
83{ 77{
84 return (subsys_create_file(&power_subsys, &auto_poweron_attr)); 78 return (sysfs_create_file(power_kobj, &auto_poweron_attr));
85} 79}
86__initcall(apo_pm_init); 80__initcall(apo_pm_init);
87#endif 81#endif
diff --git a/arch/powerpc/sysdev/ipic.c b/arch/powerpc/sysdev/ipic.c
index 05a56e55804c..e898ff4d2b97 100644
--- a/arch/powerpc/sysdev/ipic.c
+++ b/arch/powerpc/sysdev/ipic.c
@@ -725,7 +725,7 @@ unsigned int ipic_get_irq(void)
725} 725}
726 726
727static struct sysdev_class ipic_sysclass = { 727static struct sysdev_class ipic_sysclass = {
728 set_kset_name("ipic"), 728 .name = "ipic",
729}; 729};
730 730
731static struct sys_device device_ipic = { 731static struct sys_device device_ipic = {
diff --git a/arch/powerpc/sysdev/mpic.c b/arch/powerpc/sysdev/mpic.c
index e47938899a92..212a94f5d34b 100644
--- a/arch/powerpc/sysdev/mpic.c
+++ b/arch/powerpc/sysdev/mpic.c
@@ -1584,7 +1584,7 @@ static struct sysdev_class mpic_sysclass = {
1584 .resume = mpic_resume, 1584 .resume = mpic_resume,
1585 .suspend = mpic_suspend, 1585 .suspend = mpic_suspend,
1586#endif 1586#endif
1587 set_kset_name("mpic"), 1587 .name = "mpic",
1588}; 1588};
1589 1589
1590static int mpic_init_sys(void) 1590static int mpic_init_sys(void)
diff --git a/arch/powerpc/sysdev/qe_lib/qe_ic.c b/arch/powerpc/sysdev/qe_lib/qe_ic.c
index e1c0fd6dbc1a..f59444d3be75 100644
--- a/arch/powerpc/sysdev/qe_lib/qe_ic.c
+++ b/arch/powerpc/sysdev/qe_lib/qe_ic.c
@@ -483,7 +483,7 @@ int qe_ic_set_high_priority(unsigned int virq, unsigned int priority, int high)
483} 483}
484 484
485static struct sysdev_class qe_ic_sysclass = { 485static struct sysdev_class qe_ic_sysclass = {
486 set_kset_name("qe_ic"), 486 .name = "qe_ic",
487}; 487};
488 488
489static struct sys_device device_qe_ic = { 489static struct sys_device device_qe_ic = {
diff --git a/arch/ppc/syslib/ipic.c b/arch/ppc/syslib/ipic.c
index 9192777d0f78..4f163e20939e 100644
--- a/arch/ppc/syslib/ipic.c
+++ b/arch/ppc/syslib/ipic.c
@@ -614,7 +614,7 @@ int ipic_get_irq(void)
614} 614}
615 615
616static struct sysdev_class ipic_sysclass = { 616static struct sysdev_class ipic_sysclass = {
617 set_kset_name("ipic"), 617 .name = "ipic",
618}; 618};
619 619
620static struct sys_device device_ipic = { 620static struct sys_device device_ipic = {
diff --git a/arch/ppc/syslib/open_pic.c b/arch/ppc/syslib/open_pic.c
index 18ec94733293..da36522d327a 100644
--- a/arch/ppc/syslib/open_pic.c
+++ b/arch/ppc/syslib/open_pic.c
@@ -1043,7 +1043,7 @@ int openpic_resume(struct sys_device *sysdev)
1043#endif /* CONFIG_PM */ 1043#endif /* CONFIG_PM */
1044 1044
1045static struct sysdev_class openpic_sysclass = { 1045static struct sysdev_class openpic_sysclass = {
1046 set_kset_name("openpic"), 1046 .name = "openpic",
1047}; 1047};
1048 1048
1049static struct sys_device device_openpic = { 1049static struct sys_device device_openpic = {
diff --git a/arch/ppc/syslib/open_pic2.c b/arch/ppc/syslib/open_pic2.c
index d585207f9f77..449075a04798 100644
--- a/arch/ppc/syslib/open_pic2.c
+++ b/arch/ppc/syslib/open_pic2.c
@@ -666,7 +666,7 @@ int openpic2_resume(struct sys_device *sysdev)
666 666
667/* HACK ALERT */ 667/* HACK ALERT */
668static struct sysdev_class openpic2_sysclass = { 668static struct sysdev_class openpic2_sysclass = {
669 set_kset_name("openpic2"), 669 .name = "openpic2",
670}; 670};
671 671
672static struct sys_device device_openpic2 = { 672static struct sys_device device_openpic2 = {
diff --git a/arch/s390/hypfs/inode.c b/arch/s390/hypfs/inode.c
index 5245717295b8..4b010ff814c9 100644
--- a/arch/s390/hypfs/inode.c
+++ b/arch/s390/hypfs/inode.c
@@ -490,7 +490,7 @@ static struct super_operations hypfs_s_ops = {
490 .show_options = hypfs_show_options, 490 .show_options = hypfs_show_options,
491}; 491};
492 492
493static decl_subsys(s390, NULL, NULL); 493static struct kobject *s390_kobj;
494 494
495static int __init hypfs_init(void) 495static int __init hypfs_init(void)
496{ 496{
@@ -506,17 +506,18 @@ static int __init hypfs_init(void)
506 goto fail_diag; 506 goto fail_diag;
507 } 507 }
508 } 508 }
509 kobj_set_kset_s(&s390_subsys, hypervisor_subsys); 509 s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
510 rc = subsystem_register(&s390_subsys); 510 if (!s390_kobj) {
511 if (rc) 511 rc = -ENOMEM;;
512 goto fail_sysfs; 512 goto fail_sysfs;
513 }
513 rc = register_filesystem(&hypfs_type); 514 rc = register_filesystem(&hypfs_type);
514 if (rc) 515 if (rc)
515 goto fail_filesystem; 516 goto fail_filesystem;
516 return 0; 517 return 0;
517 518
518fail_filesystem: 519fail_filesystem:
519 subsystem_unregister(&s390_subsys); 520 kobject_put(s390_kobj);
520fail_sysfs: 521fail_sysfs:
521 if (!MACHINE_IS_VM) 522 if (!MACHINE_IS_VM)
522 hypfs_diag_exit(); 523 hypfs_diag_exit();
@@ -530,7 +531,7 @@ static void __exit hypfs_exit(void)
530 if (!MACHINE_IS_VM) 531 if (!MACHINE_IS_VM)
531 hypfs_diag_exit(); 532 hypfs_diag_exit();
532 unregister_filesystem(&hypfs_type); 533 unregister_filesystem(&hypfs_type);
533 subsystem_unregister(&s390_subsys); 534 kobject_put(s390_kobj);
534} 535}
535 536
536module_init(hypfs_init) 537module_init(hypfs_init)
diff --git a/arch/s390/kernel/ipl.c b/arch/s390/kernel/ipl.c
index ce0856d32500..b97694fa62ec 100644
--- a/arch/s390/kernel/ipl.c
+++ b/arch/s390/kernel/ipl.c
@@ -162,22 +162,25 @@ EXPORT_SYMBOL_GPL(diag308);
162/* SYSFS */ 162/* SYSFS */
163 163
164#define DEFINE_IPL_ATTR_RO(_prefix, _name, _format, _value) \ 164#define DEFINE_IPL_ATTR_RO(_prefix, _name, _format, _value) \
165static ssize_t sys_##_prefix##_##_name##_show(struct kset *kset, \ 165static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj, \
166 struct kobj_attribute *attr, \
166 char *page) \ 167 char *page) \
167{ \ 168{ \
168 return sprintf(page, _format, _value); \ 169 return sprintf(page, _format, _value); \
169} \ 170} \
170static struct subsys_attribute sys_##_prefix##_##_name##_attr = \ 171static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
171 __ATTR(_name, S_IRUGO, sys_##_prefix##_##_name##_show, NULL); 172 __ATTR(_name, S_IRUGO, sys_##_prefix##_##_name##_show, NULL);
172 173
173#define DEFINE_IPL_ATTR_RW(_prefix, _name, _fmt_out, _fmt_in, _value) \ 174#define DEFINE_IPL_ATTR_RW(_prefix, _name, _fmt_out, _fmt_in, _value) \
174static ssize_t sys_##_prefix##_##_name##_show(struct kset *kset, \ 175static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj, \
176 struct kobj_attribute *attr, \
175 char *page) \ 177 char *page) \
176{ \ 178{ \
177 return sprintf(page, _fmt_out, \ 179 return sprintf(page, _fmt_out, \
178 (unsigned long long) _value); \ 180 (unsigned long long) _value); \
179} \ 181} \
180static ssize_t sys_##_prefix##_##_name##_store(struct kset *kset, \ 182static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
183 struct kobj_attribute *attr, \
181 const char *buf, size_t len) \ 184 const char *buf, size_t len) \
182{ \ 185{ \
183 unsigned long long value; \ 186 unsigned long long value; \
@@ -186,25 +189,27 @@ static ssize_t sys_##_prefix##_##_name##_store(struct kset *kset, \
186 _value = value; \ 189 _value = value; \
187 return len; \ 190 return len; \
188} \ 191} \
189static struct subsys_attribute sys_##_prefix##_##_name##_attr = \ 192static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
190 __ATTR(_name,(S_IRUGO | S_IWUSR), \ 193 __ATTR(_name,(S_IRUGO | S_IWUSR), \
191 sys_##_prefix##_##_name##_show, \ 194 sys_##_prefix##_##_name##_show, \
192 sys_##_prefix##_##_name##_store); 195 sys_##_prefix##_##_name##_store);
193 196
194#define DEFINE_IPL_ATTR_STR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)\ 197#define DEFINE_IPL_ATTR_STR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)\
195static ssize_t sys_##_prefix##_##_name##_show(struct kset *kset, \ 198static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj, \
199 struct kobj_attribute *attr, \
196 char *page) \ 200 char *page) \
197{ \ 201{ \
198 return sprintf(page, _fmt_out, _value); \ 202 return sprintf(page, _fmt_out, _value); \
199} \ 203} \
200static ssize_t sys_##_prefix##_##_name##_store(struct kset *kset, \ 204static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
205 struct kobj_attribute *attr, \
201 const char *buf, size_t len) \ 206 const char *buf, size_t len) \
202{ \ 207{ \
203 if (sscanf(buf, _fmt_in, _value) != 1) \ 208 if (sscanf(buf, _fmt_in, _value) != 1) \
204 return -EINVAL; \ 209 return -EINVAL; \
205 return len; \ 210 return len; \
206} \ 211} \
207static struct subsys_attribute sys_##_prefix##_##_name##_attr = \ 212static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
208 __ATTR(_name,(S_IRUGO | S_IWUSR), \ 213 __ATTR(_name,(S_IRUGO | S_IWUSR), \
209 sys_##_prefix##_##_name##_show, \ 214 sys_##_prefix##_##_name##_show, \
210 sys_##_prefix##_##_name##_store); 215 sys_##_prefix##_##_name##_store);
@@ -270,14 +275,16 @@ void __init setup_ipl_info(void)
270struct ipl_info ipl_info; 275struct ipl_info ipl_info;
271EXPORT_SYMBOL_GPL(ipl_info); 276EXPORT_SYMBOL_GPL(ipl_info);
272 277
273static ssize_t ipl_type_show(struct kset *kset, char *page) 278static ssize_t ipl_type_show(struct kobject *kobj, struct kobj_attribute *attr,
279 char *page)
274{ 280{
275 return sprintf(page, "%s\n", ipl_type_str(ipl_info.type)); 281 return sprintf(page, "%s\n", ipl_type_str(ipl_info.type));
276} 282}
277 283
278static struct subsys_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type); 284static struct kobj_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type);
279 285
280static ssize_t sys_ipl_device_show(struct kset *kset, char *page) 286static ssize_t sys_ipl_device_show(struct kobject *kobj,
287 struct kobj_attribute *attr, char *page)
281{ 288{
282 struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START; 289 struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START;
283 290
@@ -292,7 +299,7 @@ static ssize_t sys_ipl_device_show(struct kset *kset, char *page)
292 } 299 }
293} 300}
294 301
295static struct subsys_attribute sys_ipl_device_attr = 302static struct kobj_attribute sys_ipl_device_attr =
296 __ATTR(device, S_IRUGO, sys_ipl_device_show, NULL); 303 __ATTR(device, S_IRUGO, sys_ipl_device_show, NULL);
297 304
298static ssize_t ipl_parameter_read(struct kobject *kobj, struct bin_attribute *attr, 305static ssize_t ipl_parameter_read(struct kobject *kobj, struct bin_attribute *attr,
@@ -367,7 +374,8 @@ static struct attribute_group ipl_fcp_attr_group = {
367 374
368/* CCW ipl device attributes */ 375/* CCW ipl device attributes */
369 376
370static ssize_t ipl_ccw_loadparm_show(struct kset *kset, char *page) 377static ssize_t ipl_ccw_loadparm_show(struct kobject *kobj,
378 struct kobj_attribute *attr, char *page)
371{ 379{
372 char loadparm[LOADPARM_LEN + 1] = {}; 380 char loadparm[LOADPARM_LEN + 1] = {};
373 381
@@ -379,7 +387,7 @@ static ssize_t ipl_ccw_loadparm_show(struct kset *kset, char *page)
379 return sprintf(page, "%s\n", loadparm); 387 return sprintf(page, "%s\n", loadparm);
380} 388}
381 389
382static struct subsys_attribute sys_ipl_ccw_loadparm_attr = 390static struct kobj_attribute sys_ipl_ccw_loadparm_attr =
383 __ATTR(loadparm, 0444, ipl_ccw_loadparm_show, NULL); 391 __ATTR(loadparm, 0444, ipl_ccw_loadparm_show, NULL);
384 392
385static struct attribute *ipl_ccw_attrs[] = { 393static struct attribute *ipl_ccw_attrs[] = {
@@ -418,7 +426,7 @@ static struct attribute_group ipl_unknown_attr_group = {
418 .attrs = ipl_unknown_attrs, 426 .attrs = ipl_unknown_attrs,
419}; 427};
420 428
421static decl_subsys(ipl, NULL, NULL); 429static struct kset *ipl_kset;
422 430
423/* 431/*
424 * reipl section 432 * reipl section
@@ -465,7 +473,8 @@ static void reipl_get_ascii_loadparm(char *loadparm)
465 strstrip(loadparm); 473 strstrip(loadparm);
466} 474}
467 475
468static ssize_t reipl_ccw_loadparm_show(struct kset *kset, char *page) 476static ssize_t reipl_ccw_loadparm_show(struct kobject *kobj,
477 struct kobj_attribute *attr, char *page)
469{ 478{
470 char buf[LOADPARM_LEN + 1]; 479 char buf[LOADPARM_LEN + 1];
471 480
@@ -473,7 +482,8 @@ static ssize_t reipl_ccw_loadparm_show(struct kset *kset, char *page)
473 return sprintf(page, "%s\n", buf); 482 return sprintf(page, "%s\n", buf);
474} 483}
475 484
476static ssize_t reipl_ccw_loadparm_store(struct kset *kset, 485static ssize_t reipl_ccw_loadparm_store(struct kobject *kobj,
486 struct kobj_attribute *attr,
477 const char *buf, size_t len) 487 const char *buf, size_t len)
478{ 488{
479 int i, lp_len; 489 int i, lp_len;
@@ -500,7 +510,7 @@ static ssize_t reipl_ccw_loadparm_store(struct kset *kset,
500 return len; 510 return len;
501} 511}
502 512
503static struct subsys_attribute sys_reipl_ccw_loadparm_attr = 513static struct kobj_attribute sys_reipl_ccw_loadparm_attr =
504 __ATTR(loadparm, 0644, reipl_ccw_loadparm_show, 514 __ATTR(loadparm, 0644, reipl_ccw_loadparm_show,
505 reipl_ccw_loadparm_store); 515 reipl_ccw_loadparm_store);
506 516
@@ -568,13 +578,15 @@ static int reipl_set_type(enum ipl_type type)
568 return 0; 578 return 0;
569} 579}
570 580
571static ssize_t reipl_type_show(struct kset *kset, char *page) 581static ssize_t reipl_type_show(struct kobject *kobj,
582 struct kobj_attribute *attr, char *page)
572{ 583{
573 return sprintf(page, "%s\n", ipl_type_str(reipl_type)); 584 return sprintf(page, "%s\n", ipl_type_str(reipl_type));
574} 585}
575 586
576static ssize_t reipl_type_store(struct kset *kset, const char *buf, 587static ssize_t reipl_type_store(struct kobject *kobj,
577 size_t len) 588 struct kobj_attribute *attr,
589 const char *buf, size_t len)
578{ 590{
579 int rc = -EINVAL; 591 int rc = -EINVAL;
580 592
@@ -587,10 +599,10 @@ static ssize_t reipl_type_store(struct kset *kset, const char *buf,
587 return (rc != 0) ? rc : len; 599 return (rc != 0) ? rc : len;
588} 600}
589 601
590static struct subsys_attribute reipl_type_attr = 602static struct kobj_attribute reipl_type_attr =
591 __ATTR(reipl_type, 0644, reipl_type_show, reipl_type_store); 603 __ATTR(reipl_type, 0644, reipl_type_show, reipl_type_store);
592 604
593static decl_subsys(reipl, NULL, NULL); 605static struct kset *reipl_kset;
594 606
595/* 607/*
596 * dump section 608 * dump section
@@ -663,13 +675,15 @@ static int dump_set_type(enum dump_type type)
663 return 0; 675 return 0;
664} 676}
665 677
666static ssize_t dump_type_show(struct kset *kset, char *page) 678static ssize_t dump_type_show(struct kobject *kobj,
679 struct kobj_attribute *attr, char *page)
667{ 680{
668 return sprintf(page, "%s\n", dump_type_str(dump_type)); 681 return sprintf(page, "%s\n", dump_type_str(dump_type));
669} 682}
670 683
671static ssize_t dump_type_store(struct kset *kset, const char *buf, 684static ssize_t dump_type_store(struct kobject *kobj,
672 size_t len) 685 struct kobj_attribute *attr,
686 const char *buf, size_t len)
673{ 687{
674 int rc = -EINVAL; 688 int rc = -EINVAL;
675 689
@@ -682,26 +696,28 @@ static ssize_t dump_type_store(struct kset *kset, const char *buf,
682 return (rc != 0) ? rc : len; 696 return (rc != 0) ? rc : len;
683} 697}
684 698
685static struct subsys_attribute dump_type_attr = 699static struct kobj_attribute dump_type_attr =
686 __ATTR(dump_type, 0644, dump_type_show, dump_type_store); 700 __ATTR(dump_type, 0644, dump_type_show, dump_type_store);
687 701
688static decl_subsys(dump, NULL, NULL); 702static struct kset *dump_kset;
689 703
690/* 704/*
691 * Shutdown actions section 705 * Shutdown actions section
692 */ 706 */
693 707
694static decl_subsys(shutdown_actions, NULL, NULL); 708static struct kset *shutdown_actions_kset;
695 709
696/* on panic */ 710/* on panic */
697 711
698static ssize_t on_panic_show(struct kset *kset, char *page) 712static ssize_t on_panic_show(struct kobject *kobj,
713 struct kobj_attribute *attr, char *page)
699{ 714{
700 return sprintf(page, "%s\n", shutdown_action_str(on_panic_action)); 715 return sprintf(page, "%s\n", shutdown_action_str(on_panic_action));
701} 716}
702 717
703static ssize_t on_panic_store(struct kset *kset, const char *buf, 718static ssize_t on_panic_store(struct kobject *kobj,
704 size_t len) 719 struct kobj_attribute *attr,
720 const char *buf, size_t len)
705{ 721{
706 if (strncmp(buf, SHUTDOWN_REIPL_STR, strlen(SHUTDOWN_REIPL_STR)) == 0) 722 if (strncmp(buf, SHUTDOWN_REIPL_STR, strlen(SHUTDOWN_REIPL_STR)) == 0)
707 on_panic_action = SHUTDOWN_REIPL; 723 on_panic_action = SHUTDOWN_REIPL;
@@ -717,7 +733,7 @@ static ssize_t on_panic_store(struct kset *kset, const char *buf,
717 return len; 733 return len;
718} 734}
719 735
720static struct subsys_attribute on_panic_attr = 736static struct kobj_attribute on_panic_attr =
721 __ATTR(on_panic, 0644, on_panic_show, on_panic_store); 737 __ATTR(on_panic, 0644, on_panic_show, on_panic_store);
722 738
723void do_reipl(void) 739void do_reipl(void)
@@ -814,23 +830,23 @@ static int __init ipl_register_fcp_files(void)
814{ 830{
815 int rc; 831 int rc;
816 832
817 rc = sysfs_create_group(&ipl_subsys.kobj, 833 rc = sysfs_create_group(&ipl_kset->kobj,
818 &ipl_fcp_attr_group); 834 &ipl_fcp_attr_group);
819 if (rc) 835 if (rc)
820 goto out; 836 goto out;
821 rc = sysfs_create_bin_file(&ipl_subsys.kobj, 837 rc = sysfs_create_bin_file(&ipl_kset->kobj,
822 &ipl_parameter_attr); 838 &ipl_parameter_attr);
823 if (rc) 839 if (rc)
824 goto out_ipl_parm; 840 goto out_ipl_parm;
825 rc = sysfs_create_bin_file(&ipl_subsys.kobj, 841 rc = sysfs_create_bin_file(&ipl_kset->kobj,
826 &ipl_scp_data_attr); 842 &ipl_scp_data_attr);
827 if (!rc) 843 if (!rc)
828 goto out; 844 goto out;
829 845
830 sysfs_remove_bin_file(&ipl_subsys.kobj, &ipl_parameter_attr); 846 sysfs_remove_bin_file(&ipl_kset->kobj, &ipl_parameter_attr);
831 847
832out_ipl_parm: 848out_ipl_parm:
833 sysfs_remove_group(&ipl_subsys.kobj, &ipl_fcp_attr_group); 849 sysfs_remove_group(&ipl_kset->kobj, &ipl_fcp_attr_group);
834out: 850out:
835 return rc; 851 return rc;
836} 852}
@@ -839,12 +855,12 @@ static int __init ipl_init(void)
839{ 855{
840 int rc; 856 int rc;
841 857
842 rc = firmware_register(&ipl_subsys); 858 ipl_kset = kset_create_and_add("ipl", NULL, firmware_kobj);
843 if (rc) 859 if (!ipl_kset)
844 return rc; 860 return -ENOMEM;
845 switch (ipl_info.type) { 861 switch (ipl_info.type) {
846 case IPL_TYPE_CCW: 862 case IPL_TYPE_CCW:
847 rc = sysfs_create_group(&ipl_subsys.kobj, 863 rc = sysfs_create_group(&ipl_kset->kobj,
848 &ipl_ccw_attr_group); 864 &ipl_ccw_attr_group);
849 break; 865 break;
850 case IPL_TYPE_FCP: 866 case IPL_TYPE_FCP:
@@ -852,16 +868,16 @@ static int __init ipl_init(void)
852 rc = ipl_register_fcp_files(); 868 rc = ipl_register_fcp_files();
853 break; 869 break;
854 case IPL_TYPE_NSS: 870 case IPL_TYPE_NSS:
855 rc = sysfs_create_group(&ipl_subsys.kobj, 871 rc = sysfs_create_group(&ipl_kset->kobj,
856 &ipl_nss_attr_group); 872 &ipl_nss_attr_group);
857 break; 873 break;
858 default: 874 default:
859 rc = sysfs_create_group(&ipl_subsys.kobj, 875 rc = sysfs_create_group(&ipl_kset->kobj,
860 &ipl_unknown_attr_group); 876 &ipl_unknown_attr_group);
861 break; 877 break;
862 } 878 }
863 if (rc) 879 if (rc)
864 firmware_unregister(&ipl_subsys); 880 kset_unregister(ipl_kset);
865 return rc; 881 return rc;
866} 882}
867 883
@@ -883,7 +899,7 @@ static int __init reipl_nss_init(void)
883 899
884 if (!MACHINE_IS_VM) 900 if (!MACHINE_IS_VM)
885 return 0; 901 return 0;
886 rc = sysfs_create_group(&reipl_subsys.kobj, &reipl_nss_attr_group); 902 rc = sysfs_create_group(&reipl_kset->kobj, &reipl_nss_attr_group);
887 if (rc) 903 if (rc)
888 return rc; 904 return rc;
889 strncpy(reipl_nss_name, kernel_nss_name, NSS_NAME_SIZE + 1); 905 strncpy(reipl_nss_name, kernel_nss_name, NSS_NAME_SIZE + 1);
@@ -898,7 +914,7 @@ static int __init reipl_ccw_init(void)
898 reipl_block_ccw = (void *) get_zeroed_page(GFP_KERNEL); 914 reipl_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
899 if (!reipl_block_ccw) 915 if (!reipl_block_ccw)
900 return -ENOMEM; 916 return -ENOMEM;
901 rc = sysfs_create_group(&reipl_subsys.kobj, &reipl_ccw_attr_group); 917 rc = sysfs_create_group(&reipl_kset->kobj, &reipl_ccw_attr_group);
902 if (rc) { 918 if (rc) {
903 free_page((unsigned long)reipl_block_ccw); 919 free_page((unsigned long)reipl_block_ccw);
904 return rc; 920 return rc;
@@ -936,7 +952,7 @@ static int __init reipl_fcp_init(void)
936 reipl_block_fcp = (void *) get_zeroed_page(GFP_KERNEL); 952 reipl_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
937 if (!reipl_block_fcp) 953 if (!reipl_block_fcp)
938 return -ENOMEM; 954 return -ENOMEM;
939 rc = sysfs_create_group(&reipl_subsys.kobj, &reipl_fcp_attr_group); 955 rc = sysfs_create_group(&reipl_kset->kobj, &reipl_fcp_attr_group);
940 if (rc) { 956 if (rc) {
941 free_page((unsigned long)reipl_block_fcp); 957 free_page((unsigned long)reipl_block_fcp);
942 return rc; 958 return rc;
@@ -958,12 +974,12 @@ static int __init reipl_init(void)
958{ 974{
959 int rc; 975 int rc;
960 976
961 rc = firmware_register(&reipl_subsys); 977 reipl_kset = kset_create_and_add("reipl", NULL, firmware_kobj);
962 if (rc) 978 if (!reipl_kset)
963 return rc; 979 return -ENOMEM;
964 rc = subsys_create_file(&reipl_subsys, &reipl_type_attr); 980 rc = sysfs_create_file(&reipl_kset->kobj, &reipl_type_attr.attr);
965 if (rc) { 981 if (rc) {
966 firmware_unregister(&reipl_subsys); 982 kset_unregister(reipl_kset);
967 return rc; 983 return rc;
968 } 984 }
969 rc = reipl_ccw_init(); 985 rc = reipl_ccw_init();
@@ -988,7 +1004,7 @@ static int __init dump_ccw_init(void)
988 dump_block_ccw = (void *) get_zeroed_page(GFP_KERNEL); 1004 dump_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
989 if (!dump_block_ccw) 1005 if (!dump_block_ccw)
990 return -ENOMEM; 1006 return -ENOMEM;
991 rc = sysfs_create_group(&dump_subsys.kobj, &dump_ccw_attr_group); 1007 rc = sysfs_create_group(&dump_kset->kobj, &dump_ccw_attr_group);
992 if (rc) { 1008 if (rc) {
993 free_page((unsigned long)dump_block_ccw); 1009 free_page((unsigned long)dump_block_ccw);
994 return rc; 1010 return rc;
@@ -1012,7 +1028,7 @@ static int __init dump_fcp_init(void)
1012 dump_block_fcp = (void *) get_zeroed_page(GFP_KERNEL); 1028 dump_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
1013 if (!dump_block_fcp) 1029 if (!dump_block_fcp)
1014 return -ENOMEM; 1030 return -ENOMEM;
1015 rc = sysfs_create_group(&dump_subsys.kobj, &dump_fcp_attr_group); 1031 rc = sysfs_create_group(&dump_kset->kobj, &dump_fcp_attr_group);
1016 if (rc) { 1032 if (rc) {
1017 free_page((unsigned long)dump_block_fcp); 1033 free_page((unsigned long)dump_block_fcp);
1018 return rc; 1034 return rc;
@@ -1047,12 +1063,12 @@ static int __init dump_init(void)
1047{ 1063{
1048 int rc; 1064 int rc;
1049 1065
1050 rc = firmware_register(&dump_subsys); 1066 dump_kset = kset_create_and_add("dump", NULL, firmware_kobj);
1051 if (rc) 1067 if (!dump_kset)
1052 return rc; 1068 return -ENOMEM;
1053 rc = subsys_create_file(&dump_subsys, &dump_type_attr); 1069 rc = sysfs_create_file(&dump_kset->kobj, &dump_type_attr);
1054 if (rc) { 1070 if (rc) {
1055 firmware_unregister(&dump_subsys); 1071 kset_unregister(dump_kset);
1056 return rc; 1072 return rc;
1057 } 1073 }
1058 rc = dump_ccw_init(); 1074 rc = dump_ccw_init();
@@ -1069,12 +1085,13 @@ static int __init shutdown_actions_init(void)
1069{ 1085{
1070 int rc; 1086 int rc;
1071 1087
1072 rc = firmware_register(&shutdown_actions_subsys); 1088 shutdown_actions_kset = kset_create_and_add("shutdown_actions", NULL,
1073 if (rc) 1089 firmware_kobj);
1074 return rc; 1090 if (!shutdown_actions_kset)
1075 rc = subsys_create_file(&shutdown_actions_subsys, &on_panic_attr); 1091 return -ENOMEM;
1092 rc = sysfs_create_file(&shutdown_actions_kset->kobj, &on_panic_attr);
1076 if (rc) { 1093 if (rc) {
1077 firmware_unregister(&shutdown_actions_subsys); 1094 kset_unregister(shutdown_actions_kset);
1078 return rc; 1095 return rc;
1079 } 1096 }
1080 atomic_notifier_chain_register(&panic_notifier_list, 1097 atomic_notifier_chain_register(&panic_notifier_list,
diff --git a/arch/s390/kernel/time.c b/arch/s390/kernel/time.c
index 22b800ce2126..3bbac1293be4 100644
--- a/arch/s390/kernel/time.c
+++ b/arch/s390/kernel/time.c
@@ -1145,7 +1145,7 @@ static void etr_work_fn(struct work_struct *work)
1145 * Sysfs interface functions 1145 * Sysfs interface functions
1146 */ 1146 */
1147static struct sysdev_class etr_sysclass = { 1147static struct sysdev_class etr_sysclass = {
1148 set_kset_name("etr") 1148 .name = "etr",
1149}; 1149};
1150 1150
1151static struct sys_device etr_port0_dev = { 1151static struct sys_device etr_port0_dev = {
diff --git a/arch/sh/drivers/dma/dma-sysfs.c b/arch/sh/drivers/dma/dma-sysfs.c
index eebcd4768bbf..51b57c0d1a3c 100644
--- a/arch/sh/drivers/dma/dma-sysfs.c
+++ b/arch/sh/drivers/dma/dma-sysfs.c
@@ -19,7 +19,7 @@
19#include <asm/dma.h> 19#include <asm/dma.h>
20 20
21static struct sysdev_class dma_sysclass = { 21static struct sysdev_class dma_sysclass = {
22 set_kset_name("dma"), 22 .name = "dma",
23}; 23};
24EXPORT_SYMBOL(dma_sysclass); 24EXPORT_SYMBOL(dma_sysclass);
25 25
diff --git a/arch/sh/kernel/cpu/sh4/sq.c b/arch/sh/kernel/cpu/sh4/sq.c
index b22a78c807e6..3008c00eea6b 100644
--- a/arch/sh/kernel/cpu/sh4/sq.c
+++ b/arch/sh/kernel/cpu/sh4/sq.c
@@ -341,17 +341,18 @@ static int __devinit sq_sysdev_add(struct sys_device *sysdev)
341{ 341{
342 unsigned int cpu = sysdev->id; 342 unsigned int cpu = sysdev->id;
343 struct kobject *kobj; 343 struct kobject *kobj;
344 int error;
344 345
345 sq_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL); 346 sq_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
346 if (unlikely(!sq_kobject[cpu])) 347 if (unlikely(!sq_kobject[cpu]))
347 return -ENOMEM; 348 return -ENOMEM;
348 349
349 kobj = sq_kobject[cpu]; 350 kobj = sq_kobject[cpu];
350 kobj->parent = &sysdev->kobj; 351 error = kobject_init_and_add(kobj, &ktype_percpu_entry, &sysdev->kobj,
351 kobject_set_name(kobj, "%s", "sq"); 352 "%s", "sq");
352 kobj->ktype = &ktype_percpu_entry; 353 if (!error)
353 354 kobject_uevent(kobj, KOBJ_ADD);
354 return kobject_register(kobj); 355 return error;
355} 356}
356 357
357static int __devexit sq_sysdev_remove(struct sys_device *sysdev) 358static int __devexit sq_sysdev_remove(struct sys_device *sysdev)
@@ -359,7 +360,7 @@ static int __devexit sq_sysdev_remove(struct sys_device *sysdev)
359 unsigned int cpu = sysdev->id; 360 unsigned int cpu = sysdev->id;
360 struct kobject *kobj = sq_kobject[cpu]; 361 struct kobject *kobj = sq_kobject[cpu];
361 362
362 kobject_unregister(kobj); 363 kobject_put(kobj);
363 return 0; 364 return 0;
364} 365}
365 366
diff --git a/arch/sh/kernel/time.c b/arch/sh/kernel/time.c
index a3a67d151e52..2bc04bfee738 100644
--- a/arch/sh/kernel/time.c
+++ b/arch/sh/kernel/time.c
@@ -174,7 +174,7 @@ int timer_resume(struct sys_device *dev)
174#endif 174#endif
175 175
176static struct sysdev_class timer_sysclass = { 176static struct sysdev_class timer_sysclass = {
177 set_kset_name("timer"), 177 .name = "timer",
178 .suspend = timer_suspend, 178 .suspend = timer_suspend,
179 .resume = timer_resume, 179 .resume = timer_resume,
180}; 180};
diff --git a/arch/x86/kernel/apic_32.c b/arch/x86/kernel/apic_32.c
index edb5108e5d0e..a56c782653be 100644
--- a/arch/x86/kernel/apic_32.c
+++ b/arch/x86/kernel/apic_32.c
@@ -1530,7 +1530,7 @@ static int lapic_resume(struct sys_device *dev)
1530 */ 1530 */
1531 1531
1532static struct sysdev_class lapic_sysclass = { 1532static struct sysdev_class lapic_sysclass = {
1533 set_kset_name("lapic"), 1533 .name = "lapic",
1534 .resume = lapic_resume, 1534 .resume = lapic_resume,
1535 .suspend = lapic_suspend, 1535 .suspend = lapic_suspend,
1536}; 1536};
diff --git a/arch/x86/kernel/apic_64.c b/arch/x86/kernel/apic_64.c
index f28ccb588fba..fa6cdee6d303 100644
--- a/arch/x86/kernel/apic_64.c
+++ b/arch/x86/kernel/apic_64.c
@@ -639,7 +639,7 @@ static int lapic_resume(struct sys_device *dev)
639} 639}
640 640
641static struct sysdev_class lapic_sysclass = { 641static struct sysdev_class lapic_sysclass = {
642 set_kset_name("lapic"), 642 .name = "lapic",
643 .resume = lapic_resume, 643 .resume = lapic_resume,
644 .suspend = lapic_suspend, 644 .suspend = lapic_suspend,
645}; 645};
diff --git a/arch/x86/kernel/cpu/intel_cacheinfo.c b/arch/x86/kernel/cpu/intel_cacheinfo.c
index 9f530ff43c21..8b4507b8469b 100644
--- a/arch/x86/kernel/cpu/intel_cacheinfo.c
+++ b/arch/x86/kernel/cpu/intel_cacheinfo.c
@@ -733,10 +733,8 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
733 if (unlikely(retval < 0)) 733 if (unlikely(retval < 0))
734 return retval; 734 return retval;
735 735
736 cache_kobject[cpu]->parent = &sys_dev->kobj; 736 retval = kobject_init_and_add(cache_kobject[cpu], &ktype_percpu_entry,
737 kobject_set_name(cache_kobject[cpu], "%s", "cache"); 737 &sys_dev->kobj, "%s", "cache");
738 cache_kobject[cpu]->ktype = &ktype_percpu_entry;
739 retval = kobject_register(cache_kobject[cpu]);
740 if (retval < 0) { 738 if (retval < 0) {
741 cpuid4_cache_sysfs_exit(cpu); 739 cpuid4_cache_sysfs_exit(cpu);
742 return retval; 740 return retval;
@@ -746,23 +744,23 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
746 this_object = INDEX_KOBJECT_PTR(cpu,i); 744 this_object = INDEX_KOBJECT_PTR(cpu,i);
747 this_object->cpu = cpu; 745 this_object->cpu = cpu;
748 this_object->index = i; 746 this_object->index = i;
749 this_object->kobj.parent = cache_kobject[cpu]; 747 retval = kobject_init_and_add(&(this_object->kobj),
750 kobject_set_name(&(this_object->kobj), "index%1lu", i); 748 &ktype_cache, cache_kobject[cpu],
751 this_object->kobj.ktype = &ktype_cache; 749 "index%1lu", i);
752 retval = kobject_register(&(this_object->kobj));
753 if (unlikely(retval)) { 750 if (unlikely(retval)) {
754 for (j = 0; j < i; j++) { 751 for (j = 0; j < i; j++) {
755 kobject_unregister( 752 kobject_put(&(INDEX_KOBJECT_PTR(cpu,j)->kobj));
756 &(INDEX_KOBJECT_PTR(cpu,j)->kobj));
757 } 753 }
758 kobject_unregister(cache_kobject[cpu]); 754 kobject_put(cache_kobject[cpu]);
759 cpuid4_cache_sysfs_exit(cpu); 755 cpuid4_cache_sysfs_exit(cpu);
760 break; 756 break;
761 } 757 }
758 kobject_uevent(&(this_object->kobj), KOBJ_ADD);
762 } 759 }
763 if (!retval) 760 if (!retval)
764 cpu_set(cpu, cache_dev_map); 761 cpu_set(cpu, cache_dev_map);
765 762
763 kobject_uevent(cache_kobject[cpu], KOBJ_ADD);
766 return retval; 764 return retval;
767} 765}
768 766
@@ -778,8 +776,8 @@ static void __cpuinit cache_remove_dev(struct sys_device * sys_dev)
778 cpu_clear(cpu, cache_dev_map); 776 cpu_clear(cpu, cache_dev_map);
779 777
780 for (i = 0; i < num_cache_leaves; i++) 778 for (i = 0; i < num_cache_leaves; i++)
781 kobject_unregister(&(INDEX_KOBJECT_PTR(cpu,i)->kobj)); 779 kobject_put(&(INDEX_KOBJECT_PTR(cpu,i)->kobj));
782 kobject_unregister(cache_kobject[cpu]); 780 kobject_put(cache_kobject[cpu]);
783 cpuid4_cache_sysfs_exit(cpu); 781 cpuid4_cache_sysfs_exit(cpu);
784} 782}
785 783
diff --git a/arch/x86/kernel/cpu/mcheck/mce_64.c b/arch/x86/kernel/cpu/mcheck/mce_64.c
index 4b21d29fb5aa..242e8668dbeb 100644
--- a/arch/x86/kernel/cpu/mcheck/mce_64.c
+++ b/arch/x86/kernel/cpu/mcheck/mce_64.c
@@ -745,7 +745,7 @@ static void mce_restart(void)
745 745
746static struct sysdev_class mce_sysclass = { 746static struct sysdev_class mce_sysclass = {
747 .resume = mce_resume, 747 .resume = mce_resume,
748 set_kset_name("machinecheck"), 748 .name = "machinecheck",
749}; 749};
750 750
751DEFINE_PER_CPU(struct sys_device, device_mce); 751DEFINE_PER_CPU(struct sys_device, device_mce);
diff --git a/arch/x86/kernel/cpu/mcheck/mce_amd_64.c b/arch/x86/kernel/cpu/mcheck/mce_amd_64.c
index 752fb16a817d..753588755fee 100644
--- a/arch/x86/kernel/cpu/mcheck/mce_amd_64.c
+++ b/arch/x86/kernel/cpu/mcheck/mce_amd_64.c
@@ -65,7 +65,7 @@ static struct threshold_block threshold_defaults = {
65}; 65};
66 66
67struct threshold_bank { 67struct threshold_bank {
68 struct kobject kobj; 68 struct kobject *kobj;
69 struct threshold_block *blocks; 69 struct threshold_block *blocks;
70 cpumask_t cpus; 70 cpumask_t cpus;
71}; 71};
@@ -432,10 +432,9 @@ static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
432 else 432 else
433 per_cpu(threshold_banks, cpu)[bank]->blocks = b; 433 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
434 434
435 kobject_set_name(&b->kobj, "misc%i", block); 435 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
436 b->kobj.parent = &per_cpu(threshold_banks, cpu)[bank]->kobj; 436 per_cpu(threshold_banks, cpu)[bank]->kobj,
437 b->kobj.ktype = &threshold_ktype; 437 "misc%i", block);
438 err = kobject_register(&b->kobj);
439 if (err) 438 if (err)
440 goto out_free; 439 goto out_free;
441recurse: 440recurse:
@@ -451,11 +450,13 @@ recurse:
451 if (err) 450 if (err)
452 goto out_free; 451 goto out_free;
453 452
453 kobject_uevent(&b->kobj, KOBJ_ADD);
454
454 return err; 455 return err;
455 456
456out_free: 457out_free:
457 if (b) { 458 if (b) {
458 kobject_unregister(&b->kobj); 459 kobject_put(&b->kobj);
459 kfree(b); 460 kfree(b);
460 } 461 }
461 return err; 462 return err;
@@ -489,7 +490,7 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
489 goto out; 490 goto out;
490 491
491 err = sysfs_create_link(&per_cpu(device_mce, cpu).kobj, 492 err = sysfs_create_link(&per_cpu(device_mce, cpu).kobj,
492 &b->kobj, name); 493 b->kobj, name);
493 if (err) 494 if (err)
494 goto out; 495 goto out;
495 496
@@ -505,16 +506,15 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
505 goto out; 506 goto out;
506 } 507 }
507 508
508 kobject_set_name(&b->kobj, "threshold_bank%i", bank); 509 b->kobj = kobject_create_and_add(name, &per_cpu(device_mce, cpu).kobj);
509 b->kobj.parent = &per_cpu(device_mce, cpu).kobj; 510 if (!b->kobj)
511 goto out_free;
512
510#ifndef CONFIG_SMP 513#ifndef CONFIG_SMP
511 b->cpus = CPU_MASK_ALL; 514 b->cpus = CPU_MASK_ALL;
512#else 515#else
513 b->cpus = per_cpu(cpu_core_map, cpu); 516 b->cpus = per_cpu(cpu_core_map, cpu);
514#endif 517#endif
515 err = kobject_register(&b->kobj);
516 if (err)
517 goto out_free;
518 518
519 per_cpu(threshold_banks, cpu)[bank] = b; 519 per_cpu(threshold_banks, cpu)[bank] = b;
520 520
@@ -531,7 +531,7 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
531 continue; 531 continue;
532 532
533 err = sysfs_create_link(&per_cpu(device_mce, i).kobj, 533 err = sysfs_create_link(&per_cpu(device_mce, i).kobj,
534 &b->kobj, name); 534 b->kobj, name);
535 if (err) 535 if (err)
536 goto out; 536 goto out;
537 537
@@ -581,7 +581,7 @@ static void deallocate_threshold_block(unsigned int cpu,
581 return; 581 return;
582 582
583 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) { 583 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
584 kobject_unregister(&pos->kobj); 584 kobject_put(&pos->kobj);
585 list_del(&pos->miscj); 585 list_del(&pos->miscj);
586 kfree(pos); 586 kfree(pos);
587 } 587 }
@@ -627,7 +627,7 @@ static void threshold_remove_bank(unsigned int cpu, int bank)
627 deallocate_threshold_block(cpu, bank); 627 deallocate_threshold_block(cpu, bank);
628 628
629free_out: 629free_out:
630 kobject_unregister(&b->kobj); 630 kobject_put(b->kobj);
631 kfree(b); 631 kfree(b);
632 per_cpu(threshold_banks, cpu)[bank] = NULL; 632 per_cpu(threshold_banks, cpu)[bank] = NULL;
633} 633}
diff --git a/arch/x86/kernel/cpuid.c b/arch/x86/kernel/cpuid.c
index 05c9936a16cc..d387c770c518 100644
--- a/arch/x86/kernel/cpuid.c
+++ b/arch/x86/kernel/cpuid.c
@@ -157,15 +157,15 @@ static int __cpuinit cpuid_class_cpu_callback(struct notifier_block *nfb,
157 157
158 switch (action) { 158 switch (action) {
159 case CPU_UP_PREPARE: 159 case CPU_UP_PREPARE:
160 case CPU_UP_PREPARE_FROZEN:
161 err = cpuid_device_create(cpu); 160 err = cpuid_device_create(cpu);
162 break; 161 break;
163 case CPU_UP_CANCELED: 162 case CPU_UP_CANCELED:
164 case CPU_UP_CANCELED_FROZEN:
165 case CPU_DEAD: 163 case CPU_DEAD:
166 case CPU_DEAD_FROZEN:
167 cpuid_device_destroy(cpu); 164 cpuid_device_destroy(cpu);
168 break; 165 break;
166 case CPU_UP_CANCELED_FROZEN:
167 destroy_suspended_device(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
168 break;
169 } 169 }
170 return err ? NOTIFY_BAD : NOTIFY_OK; 170 return err ? NOTIFY_BAD : NOTIFY_OK;
171} 171}
diff --git a/arch/x86/kernel/i8237.c b/arch/x86/kernel/i8237.c
index 29313832df0c..dbd6c1d1b638 100644
--- a/arch/x86/kernel/i8237.c
+++ b/arch/x86/kernel/i8237.c
@@ -51,7 +51,7 @@ static int i8237A_suspend(struct sys_device *dev, pm_message_t state)
51} 51}
52 52
53static struct sysdev_class i8237_sysdev_class = { 53static struct sysdev_class i8237_sysdev_class = {
54 set_kset_name("i8237"), 54 .name = "i8237",
55 .suspend = i8237A_suspend, 55 .suspend = i8237A_suspend,
56 .resume = i8237A_resume, 56 .resume = i8237A_resume,
57}; 57};
diff --git a/arch/x86/kernel/i8259_32.c b/arch/x86/kernel/i8259_32.c
index f634fc715c99..5f3496d01984 100644
--- a/arch/x86/kernel/i8259_32.c
+++ b/arch/x86/kernel/i8259_32.c
@@ -258,7 +258,7 @@ static int i8259A_shutdown(struct sys_device *dev)
258} 258}
259 259
260static struct sysdev_class i8259_sysdev_class = { 260static struct sysdev_class i8259_sysdev_class = {
261 set_kset_name("i8259"), 261 .name = "i8259",
262 .suspend = i8259A_suspend, 262 .suspend = i8259A_suspend,
263 .resume = i8259A_resume, 263 .resume = i8259A_resume,
264 .shutdown = i8259A_shutdown, 264 .shutdown = i8259A_shutdown,
diff --git a/arch/x86/kernel/i8259_64.c b/arch/x86/kernel/i8259_64.c
index 3f27ea0b9816..ba6d57286f56 100644
--- a/arch/x86/kernel/i8259_64.c
+++ b/arch/x86/kernel/i8259_64.c
@@ -370,7 +370,7 @@ static int i8259A_shutdown(struct sys_device *dev)
370} 370}
371 371
372static struct sysdev_class i8259_sysdev_class = { 372static struct sysdev_class i8259_sysdev_class = {
373 set_kset_name("i8259"), 373 .name = "i8259",
374 .suspend = i8259A_suspend, 374 .suspend = i8259A_suspend,
375 .resume = i8259A_resume, 375 .resume = i8259A_resume,
376 .shutdown = i8259A_shutdown, 376 .shutdown = i8259A_shutdown,
diff --git a/arch/x86/kernel/io_apic_32.c b/arch/x86/kernel/io_apic_32.c
index a6b1490e00c4..ab77f1905469 100644
--- a/arch/x86/kernel/io_apic_32.c
+++ b/arch/x86/kernel/io_apic_32.c
@@ -2401,7 +2401,7 @@ static int ioapic_resume(struct sys_device *dev)
2401} 2401}
2402 2402
2403static struct sysdev_class ioapic_sysdev_class = { 2403static struct sysdev_class ioapic_sysdev_class = {
2404 set_kset_name("ioapic"), 2404 .name = "ioapic",
2405 .suspend = ioapic_suspend, 2405 .suspend = ioapic_suspend,
2406 .resume = ioapic_resume, 2406 .resume = ioapic_resume,
2407}; 2407};
diff --git a/arch/x86/kernel/io_apic_64.c b/arch/x86/kernel/io_apic_64.c
index cbac1670c7c3..23a3ac06a23e 100644
--- a/arch/x86/kernel/io_apic_64.c
+++ b/arch/x86/kernel/io_apic_64.c
@@ -1850,7 +1850,7 @@ static int ioapic_resume(struct sys_device *dev)
1850} 1850}
1851 1851
1852static struct sysdev_class ioapic_sysdev_class = { 1852static struct sysdev_class ioapic_sysdev_class = {
1853 set_kset_name("ioapic"), 1853 .name = "ioapic",
1854 .suspend = ioapic_suspend, 1854 .suspend = ioapic_suspend,
1855 .resume = ioapic_resume, 1855 .resume = ioapic_resume,
1856}; 1856};
diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c
index ee6eba4ecfea..21f6e3c0be18 100644
--- a/arch/x86/kernel/msr.c
+++ b/arch/x86/kernel/msr.c
@@ -155,15 +155,15 @@ static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
155 155
156 switch (action) { 156 switch (action) {
157 case CPU_UP_PREPARE: 157 case CPU_UP_PREPARE:
158 case CPU_UP_PREPARE_FROZEN:
159 err = msr_device_create(cpu); 158 err = msr_device_create(cpu);
160 break; 159 break;
161 case CPU_UP_CANCELED: 160 case CPU_UP_CANCELED:
162 case CPU_UP_CANCELED_FROZEN:
163 case CPU_DEAD: 161 case CPU_DEAD:
164 case CPU_DEAD_FROZEN:
165 msr_device_destroy(cpu); 162 msr_device_destroy(cpu);
166 break; 163 break;
164 case CPU_UP_CANCELED_FROZEN:
165 destroy_suspended_device(msr_class, MKDEV(MSR_MAJOR, cpu));
166 break;
167 } 167 }
168 return err ? NOTIFY_BAD : NOTIFY_OK; 168 return err ? NOTIFY_BAD : NOTIFY_OK;
169} 169}
diff --git a/arch/x86/kernel/nmi_32.c b/arch/x86/kernel/nmi_32.c
index 852db2906921..4f4bfd3a88b6 100644
--- a/arch/x86/kernel/nmi_32.c
+++ b/arch/x86/kernel/nmi_32.c
@@ -176,7 +176,7 @@ static int lapic_nmi_resume(struct sys_device *dev)
176 176
177 177
178static struct sysdev_class nmi_sysclass = { 178static struct sysdev_class nmi_sysclass = {
179 set_kset_name("lapic_nmi"), 179 .name = "lapic_nmi",
180 .resume = lapic_nmi_resume, 180 .resume = lapic_nmi_resume,
181 .suspend = lapic_nmi_suspend, 181 .suspend = lapic_nmi_suspend,
182}; 182};
diff --git a/arch/x86/kernel/nmi_64.c b/arch/x86/kernel/nmi_64.c
index 4253c4e8849c..c3d1476b6a11 100644
--- a/arch/x86/kernel/nmi_64.c
+++ b/arch/x86/kernel/nmi_64.c
@@ -211,7 +211,7 @@ static int lapic_nmi_resume(struct sys_device *dev)
211} 211}
212 212
213static struct sysdev_class nmi_sysclass = { 213static struct sysdev_class nmi_sysclass = {
214 set_kset_name("lapic_nmi"), 214 .name = "lapic_nmi",
215 .resume = lapic_nmi_resume, 215 .resume = lapic_nmi_resume,
216 .suspend = lapic_nmi_suspend, 216 .suspend = lapic_nmi_suspend,
217}; 217};
diff --git a/arch/x86/oprofile/nmi_int.c b/arch/x86/oprofile/nmi_int.c
index 944bbcdd2b8d..c8ab79ef4276 100644
--- a/arch/x86/oprofile/nmi_int.c
+++ b/arch/x86/oprofile/nmi_int.c
@@ -51,7 +51,7 @@ static int nmi_resume(struct sys_device *dev)
51 51
52 52
53static struct sysdev_class oprofile_sysclass = { 53static struct sysdev_class oprofile_sysclass = {
54 set_kset_name("oprofile"), 54 .name = "oprofile",
55 .resume = nmi_resume, 55 .resume = nmi_resume,
56 .suspend = nmi_suspend, 56 .suspend = nmi_suspend,
57}; 57};
diff --git a/block/elevator.c b/block/elevator.c
index e452deb80395..f9736fbdab03 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -185,9 +185,7 @@ static elevator_t *elevator_alloc(struct request_queue *q,
185 185
186 eq->ops = &e->ops; 186 eq->ops = &e->ops;
187 eq->elevator_type = e; 187 eq->elevator_type = e;
188 kobject_init(&eq->kobj); 188 kobject_init(&eq->kobj, &elv_ktype);
189 kobject_set_name(&eq->kobj, "%s", "iosched");
190 eq->kobj.ktype = &elv_ktype;
191 mutex_init(&eq->sysfs_lock); 189 mutex_init(&eq->sysfs_lock);
192 190
193 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES, 191 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
@@ -931,9 +929,7 @@ int elv_register_queue(struct request_queue *q)
931 elevator_t *e = q->elevator; 929 elevator_t *e = q->elevator;
932 int error; 930 int error;
933 931
934 e->kobj.parent = &q->kobj; 932 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
935
936 error = kobject_add(&e->kobj);
937 if (!error) { 933 if (!error) {
938 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs; 934 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
939 if (attr) { 935 if (attr) {
diff --git a/block/genhd.c b/block/genhd.c
index f2ac914160d1..5e4ab4b37d9f 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -17,8 +17,10 @@
17#include <linux/buffer_head.h> 17#include <linux/buffer_head.h>
18#include <linux/mutex.h> 18#include <linux/mutex.h>
19 19
20struct kset block_subsys; 20static DEFINE_MUTEX(block_class_lock);
21static DEFINE_MUTEX(block_subsys_lock); 21#ifndef CONFIG_SYSFS_DEPRECATED
22struct kobject *block_depr;
23#endif
22 24
23/* 25/*
24 * Can be deleted altogether. Later. 26 * Can be deleted altogether. Later.
@@ -37,19 +39,17 @@ static inline int major_to_index(int major)
37} 39}
38 40
39#ifdef CONFIG_PROC_FS 41#ifdef CONFIG_PROC_FS
40
41void blkdev_show(struct seq_file *f, off_t offset) 42void blkdev_show(struct seq_file *f, off_t offset)
42{ 43{
43 struct blk_major_name *dp; 44 struct blk_major_name *dp;
44 45
45 if (offset < BLKDEV_MAJOR_HASH_SIZE) { 46 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
46 mutex_lock(&block_subsys_lock); 47 mutex_lock(&block_class_lock);
47 for (dp = major_names[offset]; dp; dp = dp->next) 48 for (dp = major_names[offset]; dp; dp = dp->next)
48 seq_printf(f, "%3d %s\n", dp->major, dp->name); 49 seq_printf(f, "%3d %s\n", dp->major, dp->name);
49 mutex_unlock(&block_subsys_lock); 50 mutex_unlock(&block_class_lock);
50 } 51 }
51} 52}
52
53#endif /* CONFIG_PROC_FS */ 53#endif /* CONFIG_PROC_FS */
54 54
55int register_blkdev(unsigned int major, const char *name) 55int register_blkdev(unsigned int major, const char *name)
@@ -57,7 +57,7 @@ int register_blkdev(unsigned int major, const char *name)
57 struct blk_major_name **n, *p; 57 struct blk_major_name **n, *p;
58 int index, ret = 0; 58 int index, ret = 0;
59 59
60 mutex_lock(&block_subsys_lock); 60 mutex_lock(&block_class_lock);
61 61
62 /* temporary */ 62 /* temporary */
63 if (major == 0) { 63 if (major == 0) {
@@ -102,7 +102,7 @@ int register_blkdev(unsigned int major, const char *name)
102 kfree(p); 102 kfree(p);
103 } 103 }
104out: 104out:
105 mutex_unlock(&block_subsys_lock); 105 mutex_unlock(&block_class_lock);
106 return ret; 106 return ret;
107} 107}
108 108
@@ -114,7 +114,7 @@ void unregister_blkdev(unsigned int major, const char *name)
114 struct blk_major_name *p = NULL; 114 struct blk_major_name *p = NULL;
115 int index = major_to_index(major); 115 int index = major_to_index(major);
116 116
117 mutex_lock(&block_subsys_lock); 117 mutex_lock(&block_class_lock);
118 for (n = &major_names[index]; *n; n = &(*n)->next) 118 for (n = &major_names[index]; *n; n = &(*n)->next)
119 if ((*n)->major == major) 119 if ((*n)->major == major)
120 break; 120 break;
@@ -124,7 +124,7 @@ void unregister_blkdev(unsigned int major, const char *name)
124 p = *n; 124 p = *n;
125 *n = p->next; 125 *n = p->next;
126 } 126 }
127 mutex_unlock(&block_subsys_lock); 127 mutex_unlock(&block_class_lock);
128 kfree(p); 128 kfree(p);
129} 129}
130 130
@@ -137,29 +137,30 @@ static struct kobj_map *bdev_map;
137 * range must be nonzero 137 * range must be nonzero
138 * The hash chain is sorted on range, so that subranges can override. 138 * The hash chain is sorted on range, so that subranges can override.
139 */ 139 */
140void blk_register_region(dev_t dev, unsigned long range, struct module *module, 140void blk_register_region(dev_t devt, unsigned long range, struct module *module,
141 struct kobject *(*probe)(dev_t, int *, void *), 141 struct kobject *(*probe)(dev_t, int *, void *),
142 int (*lock)(dev_t, void *), void *data) 142 int (*lock)(dev_t, void *), void *data)
143{ 143{
144 kobj_map(bdev_map, dev, range, module, probe, lock, data); 144 kobj_map(bdev_map, devt, range, module, probe, lock, data);
145} 145}
146 146
147EXPORT_SYMBOL(blk_register_region); 147EXPORT_SYMBOL(blk_register_region);
148 148
149void blk_unregister_region(dev_t dev, unsigned long range) 149void blk_unregister_region(dev_t devt, unsigned long range)
150{ 150{
151 kobj_unmap(bdev_map, dev, range); 151 kobj_unmap(bdev_map, devt, range);
152} 152}
153 153
154EXPORT_SYMBOL(blk_unregister_region); 154EXPORT_SYMBOL(blk_unregister_region);
155 155
156static struct kobject *exact_match(dev_t dev, int *part, void *data) 156static struct kobject *exact_match(dev_t devt, int *part, void *data)
157{ 157{
158 struct gendisk *p = data; 158 struct gendisk *p = data;
159 return &p->kobj; 159
160 return &p->dev.kobj;
160} 161}
161 162
162static int exact_lock(dev_t dev, void *data) 163static int exact_lock(dev_t devt, void *data)
163{ 164{
164 struct gendisk *p = data; 165 struct gendisk *p = data;
165 166
@@ -194,8 +195,6 @@ void unlink_gendisk(struct gendisk *disk)
194 disk->minors); 195 disk->minors);
195} 196}
196 197
197#define to_disk(obj) container_of(obj,struct gendisk,kobj)
198
199/** 198/**
200 * get_gendisk - get partitioning information for a given device 199 * get_gendisk - get partitioning information for a given device
201 * @dev: device to get partitioning information for 200 * @dev: device to get partitioning information for
@@ -203,10 +202,12 @@ void unlink_gendisk(struct gendisk *disk)
203 * This function gets the structure containing partitioning 202 * This function gets the structure containing partitioning
204 * information for the given device @dev. 203 * information for the given device @dev.
205 */ 204 */
206struct gendisk *get_gendisk(dev_t dev, int *part) 205struct gendisk *get_gendisk(dev_t devt, int *part)
207{ 206{
208 struct kobject *kobj = kobj_lookup(bdev_map, dev, part); 207 struct kobject *kobj = kobj_lookup(bdev_map, devt, part);
209 return kobj ? to_disk(kobj) : NULL; 208 struct device *dev = kobj_to_dev(kobj);
209
210 return kobj ? dev_to_disk(dev) : NULL;
210} 211}
211 212
212/* 213/*
@@ -216,13 +217,17 @@ struct gendisk *get_gendisk(dev_t dev, int *part)
216 */ 217 */
217void __init printk_all_partitions(void) 218void __init printk_all_partitions(void)
218{ 219{
219 int n; 220 struct device *dev;
220 struct gendisk *sgp; 221 struct gendisk *sgp;
222 char buf[BDEVNAME_SIZE];
223 int n;
221 224
222 mutex_lock(&block_subsys_lock); 225 mutex_lock(&block_class_lock);
223 /* For each block device... */ 226 /* For each block device... */
224 list_for_each_entry(sgp, &block_subsys.list, kobj.entry) { 227 list_for_each_entry(dev, &block_class.devices, node) {
225 char buf[BDEVNAME_SIZE]; 228 if (dev->type != &disk_type)
229 continue;
230 sgp = dev_to_disk(dev);
226 /* 231 /*
227 * Don't show empty devices or things that have been surpressed 232 * Don't show empty devices or things that have been surpressed
228 */ 233 */
@@ -255,38 +260,46 @@ void __init printk_all_partitions(void)
255 sgp->major, n + 1 + sgp->first_minor, 260 sgp->major, n + 1 + sgp->first_minor,
256 (unsigned long long)sgp->part[n]->nr_sects >> 1, 261 (unsigned long long)sgp->part[n]->nr_sects >> 1,
257 disk_name(sgp, n + 1, buf)); 262 disk_name(sgp, n + 1, buf));
258 } /* partition subloop */ 263 }
259 } /* Block device loop */ 264 }
260 265
261 mutex_unlock(&block_subsys_lock); 266 mutex_unlock(&block_class_lock);
262 return;
263} 267}
264 268
265#ifdef CONFIG_PROC_FS 269#ifdef CONFIG_PROC_FS
266/* iterator */ 270/* iterator */
267static void *part_start(struct seq_file *part, loff_t *pos) 271static void *part_start(struct seq_file *part, loff_t *pos)
268{ 272{
269 struct list_head *p; 273 loff_t k = *pos;
270 loff_t l = *pos; 274 struct device *dev;
271 275
272 mutex_lock(&block_subsys_lock); 276 mutex_lock(&block_class_lock);
273 list_for_each(p, &block_subsys.list) 277 list_for_each_entry(dev, &block_class.devices, node) {
274 if (!l--) 278 if (dev->type != &disk_type)
275 return list_entry(p, struct gendisk, kobj.entry); 279 continue;
280 if (!k--)
281 return dev_to_disk(dev);
282 }
276 return NULL; 283 return NULL;
277} 284}
278 285
279static void *part_next(struct seq_file *part, void *v, loff_t *pos) 286static void *part_next(struct seq_file *part, void *v, loff_t *pos)
280{ 287{
281 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; 288 struct gendisk *gp = v;
289 struct device *dev;
282 ++*pos; 290 ++*pos;
283 return p==&block_subsys.list ? NULL : 291 list_for_each_entry(dev, &gp->dev.node, node) {
284 list_entry(p, struct gendisk, kobj.entry); 292 if (&dev->node == &block_class.devices)
293 return NULL;
294 if (dev->type == &disk_type)
295 return dev_to_disk(dev);
296 }
297 return NULL;
285} 298}
286 299
287static void part_stop(struct seq_file *part, void *v) 300static void part_stop(struct seq_file *part, void *v)
288{ 301{
289 mutex_unlock(&block_subsys_lock); 302 mutex_unlock(&block_class_lock);
290} 303}
291 304
292static int show_partition(struct seq_file *part, void *v) 305static int show_partition(struct seq_file *part, void *v)
@@ -295,7 +308,7 @@ static int show_partition(struct seq_file *part, void *v)
295 int n; 308 int n;
296 char buf[BDEVNAME_SIZE]; 309 char buf[BDEVNAME_SIZE];
297 310
298 if (&sgp->kobj.entry == block_subsys.list.next) 311 if (&sgp->dev.node == block_class.devices.next)
299 seq_puts(part, "major minor #blocks name\n\n"); 312 seq_puts(part, "major minor #blocks name\n\n");
300 313
301 /* Don't show non-partitionable removeable devices or empty devices */ 314 /* Don't show non-partitionable removeable devices or empty devices */
@@ -325,110 +338,81 @@ static int show_partition(struct seq_file *part, void *v)
325} 338}
326 339
327struct seq_operations partitions_op = { 340struct seq_operations partitions_op = {
328 .start =part_start, 341 .start = part_start,
329 .next = part_next, 342 .next = part_next,
330 .stop = part_stop, 343 .stop = part_stop,
331 .show = show_partition 344 .show = show_partition
332}; 345};
333#endif 346#endif
334 347
335 348
336extern int blk_dev_init(void); 349extern int blk_dev_init(void);
337 350
338static struct kobject *base_probe(dev_t dev, int *part, void *data) 351static struct kobject *base_probe(dev_t devt, int *part, void *data)
339{ 352{
340 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0) 353 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
341 /* Make old-style 2.4 aliases work */ 354 /* Make old-style 2.4 aliases work */
342 request_module("block-major-%d", MAJOR(dev)); 355 request_module("block-major-%d", MAJOR(devt));
343 return NULL; 356 return NULL;
344} 357}
345 358
346static int __init genhd_device_init(void) 359static int __init genhd_device_init(void)
347{ 360{
348 int err; 361 class_register(&block_class);
349 362 bdev_map = kobj_map_init(base_probe, &block_class_lock);
350 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
351 blk_dev_init(); 363 blk_dev_init();
352 err = subsystem_register(&block_subsys); 364
353 if (err < 0) 365#ifndef CONFIG_SYSFS_DEPRECATED
354 printk(KERN_WARNING "%s: subsystem_register error: %d\n", 366 /* create top-level block dir */
355 __FUNCTION__, err); 367 block_depr = kobject_create_and_add("block", NULL);
356 return err; 368#endif
369 return 0;
357} 370}
358 371
359subsys_initcall(genhd_device_init); 372subsys_initcall(genhd_device_init);
360 373
361 374static ssize_t disk_range_show(struct device *dev,
362 375 struct device_attribute *attr, char *buf)
363/*
364 * kobject & sysfs bindings for block devices
365 */
366static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
367 char *page)
368{ 376{
369 struct gendisk *disk = to_disk(kobj); 377 struct gendisk *disk = dev_to_disk(dev);
370 struct disk_attribute *disk_attr =
371 container_of(attr,struct disk_attribute,attr);
372 ssize_t ret = -EIO;
373 378
374 if (disk_attr->show) 379 return sprintf(buf, "%d\n", disk->minors);
375 ret = disk_attr->show(disk,page);
376 return ret;
377} 380}
378 381
379static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr, 382static ssize_t disk_removable_show(struct device *dev,
380 const char *page, size_t count) 383 struct device_attribute *attr, char *buf)
381{ 384{
382 struct gendisk *disk = to_disk(kobj); 385 struct gendisk *disk = dev_to_disk(dev);
383 struct disk_attribute *disk_attr =
384 container_of(attr,struct disk_attribute,attr);
385 ssize_t ret = 0;
386 386
387 if (disk_attr->store) 387 return sprintf(buf, "%d\n",
388 ret = disk_attr->store(disk, page, count); 388 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
389 return ret;
390} 389}
391 390
392static struct sysfs_ops disk_sysfs_ops = { 391static ssize_t disk_size_show(struct device *dev,
393 .show = &disk_attr_show, 392 struct device_attribute *attr, char *buf)
394 .store = &disk_attr_store,
395};
396
397static ssize_t disk_uevent_store(struct gendisk * disk,
398 const char *buf, size_t count)
399{
400 kobject_uevent(&disk->kobj, KOBJ_ADD);
401 return count;
402}
403static ssize_t disk_dev_read(struct gendisk * disk, char *page)
404{
405 dev_t base = MKDEV(disk->major, disk->first_minor);
406 return print_dev_t(page, base);
407}
408static ssize_t disk_range_read(struct gendisk * disk, char *page)
409{ 393{
410 return sprintf(page, "%d\n", disk->minors); 394 struct gendisk *disk = dev_to_disk(dev);
411}
412static ssize_t disk_removable_read(struct gendisk * disk, char *page)
413{
414 return sprintf(page, "%d\n",
415 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
416 395
396 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
417} 397}
418static ssize_t disk_size_read(struct gendisk * disk, char *page) 398
419{ 399static ssize_t disk_capability_show(struct device *dev,
420 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk)); 400 struct device_attribute *attr, char *buf)
421}
422static ssize_t disk_capability_read(struct gendisk *disk, char *page)
423{ 401{
424 return sprintf(page, "%x\n", disk->flags); 402 struct gendisk *disk = dev_to_disk(dev);
403
404 return sprintf(buf, "%x\n", disk->flags);
425} 405}
426static ssize_t disk_stats_read(struct gendisk * disk, char *page) 406
407static ssize_t disk_stat_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
427{ 409{
410 struct gendisk *disk = dev_to_disk(dev);
411
428 preempt_disable(); 412 preempt_disable();
429 disk_round_stats(disk); 413 disk_round_stats(disk);
430 preempt_enable(); 414 preempt_enable();
431 return sprintf(page, 415 return sprintf(buf,
432 "%8lu %8lu %8llu %8u " 416 "%8lu %8lu %8llu %8u "
433 "%8lu %8lu %8llu %8u " 417 "%8lu %8lu %8llu %8u "
434 "%8u %8u %8u" 418 "%8u %8u %8u"
@@ -445,40 +429,21 @@ static ssize_t disk_stats_read(struct gendisk * disk, char *page)
445 jiffies_to_msecs(disk_stat_read(disk, io_ticks)), 429 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
446 jiffies_to_msecs(disk_stat_read(disk, time_in_queue))); 430 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
447} 431}
448static struct disk_attribute disk_attr_uevent = {
449 .attr = {.name = "uevent", .mode = S_IWUSR },
450 .store = disk_uevent_store
451};
452static struct disk_attribute disk_attr_dev = {
453 .attr = {.name = "dev", .mode = S_IRUGO },
454 .show = disk_dev_read
455};
456static struct disk_attribute disk_attr_range = {
457 .attr = {.name = "range", .mode = S_IRUGO },
458 .show = disk_range_read
459};
460static struct disk_attribute disk_attr_removable = {
461 .attr = {.name = "removable", .mode = S_IRUGO },
462 .show = disk_removable_read
463};
464static struct disk_attribute disk_attr_size = {
465 .attr = {.name = "size", .mode = S_IRUGO },
466 .show = disk_size_read
467};
468static struct disk_attribute disk_attr_capability = {
469 .attr = {.name = "capability", .mode = S_IRUGO },
470 .show = disk_capability_read
471};
472static struct disk_attribute disk_attr_stat = {
473 .attr = {.name = "stat", .mode = S_IRUGO },
474 .show = disk_stats_read
475};
476 432
477#ifdef CONFIG_FAIL_MAKE_REQUEST 433#ifdef CONFIG_FAIL_MAKE_REQUEST
434static ssize_t disk_fail_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
436{
437 struct gendisk *disk = dev_to_disk(dev);
438
439 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
440}
478 441
479static ssize_t disk_fail_store(struct gendisk * disk, 442static ssize_t disk_fail_store(struct device *dev,
443 struct device_attribute *attr,
480 const char *buf, size_t count) 444 const char *buf, size_t count)
481{ 445{
446 struct gendisk *disk = dev_to_disk(dev);
482 int i; 447 int i;
483 448
484 if (count > 0 && sscanf(buf, "%d", &i) > 0) { 449 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
@@ -490,136 +455,100 @@ static ssize_t disk_fail_store(struct gendisk * disk,
490 455
491 return count; 456 return count;
492} 457}
493static ssize_t disk_fail_read(struct gendisk * disk, char *page)
494{
495 return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
496}
497static struct disk_attribute disk_attr_fail = {
498 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
499 .store = disk_fail_store,
500 .show = disk_fail_read
501};
502 458
503#endif 459#endif
504 460
505static struct attribute * default_attrs[] = { 461static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
506 &disk_attr_uevent.attr, 462static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
507 &disk_attr_dev.attr, 463static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
508 &disk_attr_range.attr, 464static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
509 &disk_attr_removable.attr, 465static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
510 &disk_attr_size.attr, 466#ifdef CONFIG_FAIL_MAKE_REQUEST
511 &disk_attr_stat.attr, 467static struct device_attribute dev_attr_fail =
512 &disk_attr_capability.attr, 468 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
469#endif
470
471static struct attribute *disk_attrs[] = {
472 &dev_attr_range.attr,
473 &dev_attr_removable.attr,
474 &dev_attr_size.attr,
475 &dev_attr_capability.attr,
476 &dev_attr_stat.attr,
513#ifdef CONFIG_FAIL_MAKE_REQUEST 477#ifdef CONFIG_FAIL_MAKE_REQUEST
514 &disk_attr_fail.attr, 478 &dev_attr_fail.attr,
515#endif 479#endif
516 NULL, 480 NULL
481};
482
483static struct attribute_group disk_attr_group = {
484 .attrs = disk_attrs,
517}; 485};
518 486
519static void disk_release(struct kobject * kobj) 487static struct attribute_group *disk_attr_groups[] = {
488 &disk_attr_group,
489 NULL
490};
491
492static void disk_release(struct device *dev)
520{ 493{
521 struct gendisk *disk = to_disk(kobj); 494 struct gendisk *disk = dev_to_disk(dev);
495
522 kfree(disk->random); 496 kfree(disk->random);
523 kfree(disk->part); 497 kfree(disk->part);
524 free_disk_stats(disk); 498 free_disk_stats(disk);
525 kfree(disk); 499 kfree(disk);
526} 500}
527 501struct class block_class = {
528static struct kobj_type ktype_block = { 502 .name = "block",
529 .release = disk_release,
530 .sysfs_ops = &disk_sysfs_ops,
531 .default_attrs = default_attrs,
532}; 503};
533 504
534extern struct kobj_type ktype_part; 505struct device_type disk_type = {
535 506 .name = "disk",
536static int block_uevent_filter(struct kset *kset, struct kobject *kobj) 507 .groups = disk_attr_groups,
537{ 508 .release = disk_release,
538 struct kobj_type *ktype = get_ktype(kobj);
539
540 return ((ktype == &ktype_block) || (ktype == &ktype_part));
541}
542
543static int block_uevent(struct kset *kset, struct kobject *kobj,
544 struct kobj_uevent_env *env)
545{
546 struct kobj_type *ktype = get_ktype(kobj);
547 struct device *physdev;
548 struct gendisk *disk;
549 struct hd_struct *part;
550
551 if (ktype == &ktype_block) {
552 disk = container_of(kobj, struct gendisk, kobj);
553 add_uevent_var(env, "MINOR=%u", disk->first_minor);
554 } else if (ktype == &ktype_part) {
555 disk = container_of(kobj->parent, struct gendisk, kobj);
556 part = container_of(kobj, struct hd_struct, kobj);
557 add_uevent_var(env, "MINOR=%u",
558 disk->first_minor + part->partno);
559 } else
560 return 0;
561
562 add_uevent_var(env, "MAJOR=%u", disk->major);
563
564 /* add physical device, backing this device */
565 physdev = disk->driverfs_dev;
566 if (physdev) {
567 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
568
569 add_uevent_var(env, "PHYSDEVPATH=%s", path);
570 kfree(path);
571
572 if (physdev->bus)
573 add_uevent_var(env, "PHYSDEVBUS=%s", physdev->bus->name);
574
575 if (physdev->driver)
576 add_uevent_var(env, physdev->driver->name);
577 }
578
579 return 0;
580}
581
582static struct kset_uevent_ops block_uevent_ops = {
583 .filter = block_uevent_filter,
584 .uevent = block_uevent,
585}; 509};
586 510
587decl_subsys(block, &ktype_block, &block_uevent_ops);
588
589/* 511/*
590 * aggregate disk stat collector. Uses the same stats that the sysfs 512 * aggregate disk stat collector. Uses the same stats that the sysfs
591 * entries do, above, but makes them available through one seq_file. 513 * entries do, above, but makes them available through one seq_file.
592 * Watching a few disks may be efficient through sysfs, but watching
593 * all of them will be more efficient through this interface.
594 * 514 *
595 * The output looks suspiciously like /proc/partitions with a bunch of 515 * The output looks suspiciously like /proc/partitions with a bunch of
596 * extra fields. 516 * extra fields.
597 */ 517 */
598 518
599/* iterator */
600static void *diskstats_start(struct seq_file *part, loff_t *pos) 519static void *diskstats_start(struct seq_file *part, loff_t *pos)
601{ 520{
602 loff_t k = *pos; 521 loff_t k = *pos;
603 struct list_head *p; 522 struct device *dev;
604 523
605 mutex_lock(&block_subsys_lock); 524 mutex_lock(&block_class_lock);
606 list_for_each(p, &block_subsys.list) 525 list_for_each_entry(dev, &block_class.devices, node) {
526 if (dev->type != &disk_type)
527 continue;
607 if (!k--) 528 if (!k--)
608 return list_entry(p, struct gendisk, kobj.entry); 529 return dev_to_disk(dev);
530 }
609 return NULL; 531 return NULL;
610} 532}
611 533
612static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos) 534static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
613{ 535{
614 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; 536 struct gendisk *gp = v;
537 struct device *dev;
538
615 ++*pos; 539 ++*pos;
616 return p==&block_subsys.list ? NULL : 540 list_for_each_entry(dev, &gp->dev.node, node) {
617 list_entry(p, struct gendisk, kobj.entry); 541 if (&dev->node == &block_class.devices)
542 return NULL;
543 if (dev->type == &disk_type)
544 return dev_to_disk(dev);
545 }
546 return NULL;
618} 547}
619 548
620static void diskstats_stop(struct seq_file *part, void *v) 549static void diskstats_stop(struct seq_file *part, void *v)
621{ 550{
622 mutex_unlock(&block_subsys_lock); 551 mutex_unlock(&block_class_lock);
623} 552}
624 553
625static int diskstats_show(struct seq_file *s, void *v) 554static int diskstats_show(struct seq_file *s, void *v)
@@ -629,7 +558,7 @@ static int diskstats_show(struct seq_file *s, void *v)
629 int n = 0; 558 int n = 0;
630 559
631 /* 560 /*
632 if (&sgp->kobj.entry == block_subsys.kset.list.next) 561 if (&gp->dev.kobj.entry == block_class.devices.next)
633 seq_puts(s, "major minor name" 562 seq_puts(s, "major minor name"
634 " rio rmerge rsect ruse wio wmerge " 563 " rio rmerge rsect ruse wio wmerge "
635 "wsect wuse running use aveq" 564 "wsect wuse running use aveq"
@@ -683,7 +612,7 @@ static void media_change_notify_thread(struct work_struct *work)
683 * set enviroment vars to indicate which event this is for 612 * set enviroment vars to indicate which event this is for
684 * so that user space will know to go check the media status. 613 * so that user space will know to go check the media status.
685 */ 614 */
686 kobject_uevent_env(&gd->kobj, KOBJ_CHANGE, envp); 615 kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
687 put_device(gd->driverfs_dev); 616 put_device(gd->driverfs_dev);
688} 617}
689 618
@@ -694,6 +623,25 @@ void genhd_media_change_notify(struct gendisk *disk)
694} 623}
695EXPORT_SYMBOL_GPL(genhd_media_change_notify); 624EXPORT_SYMBOL_GPL(genhd_media_change_notify);
696 625
626dev_t blk_lookup_devt(const char *name)
627{
628 struct device *dev;
629 dev_t devt = MKDEV(0, 0);
630
631 mutex_lock(&block_class_lock);
632 list_for_each_entry(dev, &block_class.devices, node) {
633 if (strcmp(dev->bus_id, name) == 0) {
634 devt = dev->devt;
635 break;
636 }
637 }
638 mutex_unlock(&block_class_lock);
639
640 return devt;
641}
642
643EXPORT_SYMBOL(blk_lookup_devt);
644
697struct gendisk *alloc_disk(int minors) 645struct gendisk *alloc_disk(int minors)
698{ 646{
699 return alloc_disk_node(minors, -1); 647 return alloc_disk_node(minors, -1);
@@ -721,9 +669,10 @@ struct gendisk *alloc_disk_node(int minors, int node_id)
721 } 669 }
722 } 670 }
723 disk->minors = minors; 671 disk->minors = minors;
724 kobj_set_kset_s(disk,block_subsys);
725 kobject_init(&disk->kobj);
726 rand_initialize_disk(disk); 672 rand_initialize_disk(disk);
673 disk->dev.class = &block_class;
674 disk->dev.type = &disk_type;
675 device_initialize(&disk->dev);
727 INIT_WORK(&disk->async_notify, 676 INIT_WORK(&disk->async_notify,
728 media_change_notify_thread); 677 media_change_notify_thread);
729 } 678 }
@@ -743,7 +692,7 @@ struct kobject *get_disk(struct gendisk *disk)
743 owner = disk->fops->owner; 692 owner = disk->fops->owner;
744 if (owner && !try_module_get(owner)) 693 if (owner && !try_module_get(owner))
745 return NULL; 694 return NULL;
746 kobj = kobject_get(&disk->kobj); 695 kobj = kobject_get(&disk->dev.kobj);
747 if (kobj == NULL) { 696 if (kobj == NULL) {
748 module_put(owner); 697 module_put(owner);
749 return NULL; 698 return NULL;
@@ -757,7 +706,7 @@ EXPORT_SYMBOL(get_disk);
757void put_disk(struct gendisk *disk) 706void put_disk(struct gendisk *disk)
758{ 707{
759 if (disk) 708 if (disk)
760 kobject_put(&disk->kobj); 709 kobject_put(&disk->dev.kobj);
761} 710}
762 711
763EXPORT_SYMBOL(put_disk); 712EXPORT_SYMBOL(put_disk);
diff --git a/block/ll_rw_blk.c b/block/ll_rw_blk.c
index 8b919940b2ab..5ccec8aa964b 100644
--- a/block/ll_rw_blk.c
+++ b/block/ll_rw_blk.c
@@ -1862,9 +1862,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
1862 1862
1863 init_timer(&q->unplug_timer); 1863 init_timer(&q->unplug_timer);
1864 1864
1865 kobject_set_name(&q->kobj, "%s", "queue"); 1865 kobject_init(&q->kobj, &queue_ktype);
1866 q->kobj.ktype = &queue_ktype;
1867 kobject_init(&q->kobj);
1868 1866
1869 mutex_init(&q->sysfs_lock); 1867 mutex_init(&q->sysfs_lock);
1870 1868
@@ -4182,9 +4180,8 @@ int blk_register_queue(struct gendisk *disk)
4182 if (!q || !q->request_fn) 4180 if (!q || !q->request_fn)
4183 return -ENXIO; 4181 return -ENXIO;
4184 4182
4185 q->kobj.parent = kobject_get(&disk->kobj); 4183 ret = kobject_add(&q->kobj, kobject_get(&disk->dev.kobj),
4186 4184 "%s", "queue");
4187 ret = kobject_add(&q->kobj);
4188 if (ret < 0) 4185 if (ret < 0)
4189 return ret; 4186 return ret;
4190 4187
@@ -4209,6 +4206,6 @@ void blk_unregister_queue(struct gendisk *disk)
4209 4206
4210 kobject_uevent(&q->kobj, KOBJ_REMOVE); 4207 kobject_uevent(&q->kobj, KOBJ_REMOVE);
4211 kobject_del(&q->kobj); 4208 kobject_del(&q->kobj);
4212 kobject_put(&disk->kobj); 4209 kobject_put(&disk->dev.kobj);
4213 } 4210 }
4214} 4211}
diff --git a/drivers/acpi/bus.c b/drivers/acpi/bus.c
index f4487c38d9f2..1b4cf984b081 100644
--- a/drivers/acpi/bus.c
+++ b/drivers/acpi/bus.c
@@ -743,7 +743,7 @@ static int __init acpi_bus_init(void)
743 return -ENODEV; 743 return -ENODEV;
744} 744}
745 745
746decl_subsys(acpi, NULL, NULL); 746struct kobject *acpi_kobj;
747 747
748static int __init acpi_init(void) 748static int __init acpi_init(void)
749{ 749{
@@ -755,10 +755,11 @@ static int __init acpi_init(void)
755 return -ENODEV; 755 return -ENODEV;
756 } 756 }
757 757
758 result = firmware_register(&acpi_subsys); 758 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
759 if (result < 0) 759 if (!acpi_kobj) {
760 printk(KERN_WARNING "%s: firmware_register error: %d\n", 760 printk(KERN_WARNING "%s: kset create error\n", __FUNCTION__);
761 __FUNCTION__, result); 761 acpi_kobj = NULL;
762 }
762 763
763 result = acpi_bus_init(); 764 result = acpi_bus_init();
764 765
diff --git a/drivers/acpi/pci_link.c b/drivers/acpi/pci_link.c
index c9f526e55392..5400ea173f6f 100644
--- a/drivers/acpi/pci_link.c
+++ b/drivers/acpi/pci_link.c
@@ -911,7 +911,7 @@ __setup("acpi_irq_balance", acpi_irq_balance_set);
911 911
912/* FIXME: we will remove this interface after all drivers call pci_disable_device */ 912/* FIXME: we will remove this interface after all drivers call pci_disable_device */
913static struct sysdev_class irqrouter_sysdev_class = { 913static struct sysdev_class irqrouter_sysdev_class = {
914 set_kset_name("irqrouter"), 914 .name = "irqrouter",
915 .resume = irqrouter_resume, 915 .resume = irqrouter_resume,
916}; 916};
917 917
diff --git a/drivers/acpi/system.c b/drivers/acpi/system.c
index edee2806e37b..5ffe0ea18967 100644
--- a/drivers/acpi/system.c
+++ b/drivers/acpi/system.c
@@ -58,7 +58,7 @@ module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
58 FS Interface (/sys) 58 FS Interface (/sys)
59 -------------------------------------------------------------------------- */ 59 -------------------------------------------------------------------------- */
60static LIST_HEAD(acpi_table_attr_list); 60static LIST_HEAD(acpi_table_attr_list);
61static struct kobject tables_kobj; 61static struct kobject *tables_kobj;
62 62
63struct acpi_table_attr { 63struct acpi_table_attr {
64 struct bin_attribute attr; 64 struct bin_attribute attr;
@@ -135,11 +135,9 @@ static int acpi_system_sysfs_init(void)
135 int table_index = 0; 135 int table_index = 0;
136 int result; 136 int result;
137 137
138 tables_kobj.parent = &acpi_subsys.kobj; 138 tables_kobj = kobject_create_and_add("tables", acpi_kobj);
139 kobject_set_name(&tables_kobj, "tables"); 139 if (!tables_kobj)
140 result = kobject_register(&tables_kobj); 140 return -ENOMEM;
141 if (result)
142 return result;
143 141
144 do { 142 do {
145 result = acpi_get_table_by_index(table_index, &table_header); 143 result = acpi_get_table_by_index(table_index, &table_header);
@@ -153,7 +151,7 @@ static int acpi_system_sysfs_init(void)
153 151
154 acpi_table_attr_init(table_attr, table_header); 152 acpi_table_attr_init(table_attr, table_header);
155 result = 153 result =
156 sysfs_create_bin_file(&tables_kobj, 154 sysfs_create_bin_file(tables_kobj,
157 &table_attr->attr); 155 &table_attr->attr);
158 if (result) { 156 if (result) {
159 kfree(table_attr); 157 kfree(table_attr);
@@ -163,6 +161,7 @@ static int acpi_system_sysfs_init(void)
163 &acpi_table_attr_list); 161 &acpi_table_attr_list);
164 } 162 }
165 } while (!result); 163 } while (!result);
164 kobject_uevent(tables_kobj, KOBJ_ADD);
166 165
167 return 0; 166 return 0;
168} 167}
diff --git a/drivers/base/Makefile b/drivers/base/Makefile
index b39ea3f59c9b..63e09c015ca0 100644
--- a/drivers/base/Makefile
+++ b/drivers/base/Makefile
@@ -11,6 +11,9 @@ obj-$(CONFIG_FW_LOADER) += firmware_class.o
11obj-$(CONFIG_NUMA) += node.o 11obj-$(CONFIG_NUMA) += node.o
12obj-$(CONFIG_MEMORY_HOTPLUG_SPARSE) += memory.o 12obj-$(CONFIG_MEMORY_HOTPLUG_SPARSE) += memory.o
13obj-$(CONFIG_SMP) += topology.o 13obj-$(CONFIG_SMP) += topology.o
14ifeq ($(CONFIG_SYSFS),y)
15obj-$(CONFIG_MODULES) += module.o
16endif
14obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o 17obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o
15 18
16ifeq ($(CONFIG_DEBUG_DRIVER),y) 19ifeq ($(CONFIG_DEBUG_DRIVER),y)
diff --git a/drivers/base/attribute_container.c b/drivers/base/attribute_container.c
index 7370d7cf5988..d4dfb97de3b0 100644
--- a/drivers/base/attribute_container.c
+++ b/drivers/base/attribute_container.c
@@ -61,7 +61,7 @@ attribute_container_classdev_to_container(struct class_device *classdev)
61} 61}
62EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container); 62EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
63 63
64static struct list_head attribute_container_list; 64static LIST_HEAD(attribute_container_list);
65 65
66static DEFINE_MUTEX(attribute_container_mutex); 66static DEFINE_MUTEX(attribute_container_mutex);
67 67
@@ -429,10 +429,3 @@ attribute_container_find_class_device(struct attribute_container *cont,
429 return cdev; 429 return cdev;
430} 430}
431EXPORT_SYMBOL_GPL(attribute_container_find_class_device); 431EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
432
433int __init
434attribute_container_init(void)
435{
436 INIT_LIST_HEAD(&attribute_container_list);
437 return 0;
438}
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 10b2fb6c9ce6..c0444146c09a 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -1,6 +1,42 @@
1 1
2/* initialisation functions */ 2/**
3 * struct bus_type_private - structure to hold the private to the driver core portions of the bus_type structure.
4 *
5 * @subsys - the struct kset that defines this bus. This is the main kobject
6 * @drivers_kset - the list of drivers associated with this bus
7 * @devices_kset - the list of devices associated with this bus
8 * @klist_devices - the klist to iterate over the @devices_kset
9 * @klist_drivers - the klist to iterate over the @drivers_kset
10 * @bus_notifier - the bus notifier list for anything that cares about things
11 * on this bus.
12 * @bus - pointer back to the struct bus_type that this structure is associated
13 * with.
14 *
15 * This structure is the one that is the actual kobject allowing struct
16 * bus_type to be statically allocated safely. Nothing outside of the driver
17 * core should ever touch these fields.
18 */
19struct bus_type_private {
20 struct kset subsys;
21 struct kset *drivers_kset;
22 struct kset *devices_kset;
23 struct klist klist_devices;
24 struct klist klist_drivers;
25 struct blocking_notifier_head bus_notifier;
26 unsigned int drivers_autoprobe:1;
27 struct bus_type *bus;
28};
29
30struct driver_private {
31 struct kobject kobj;
32 struct klist klist_devices;
33 struct klist_node knode_bus;
34 struct module_kobject *mkobj;
35 struct device_driver *driver;
36};
37#define to_driver(obj) container_of(obj, struct driver_private, kobj)
3 38
39/* initialisation functions */
4extern int devices_init(void); 40extern int devices_init(void);
5extern int buses_init(void); 41extern int buses_init(void);
6extern int classes_init(void); 42extern int classes_init(void);
@@ -13,17 +49,16 @@ static inline int hypervisor_init(void) { return 0; }
13extern int platform_bus_init(void); 49extern int platform_bus_init(void);
14extern int system_bus_init(void); 50extern int system_bus_init(void);
15extern int cpu_dev_init(void); 51extern int cpu_dev_init(void);
16extern int attribute_container_init(void);
17 52
18extern int bus_add_device(struct device * dev); 53extern int bus_add_device(struct device *dev);
19extern void bus_attach_device(struct device * dev); 54extern void bus_attach_device(struct device *dev);
20extern void bus_remove_device(struct device * dev); 55extern void bus_remove_device(struct device *dev);
21 56
22extern int bus_add_driver(struct device_driver *); 57extern int bus_add_driver(struct device_driver *drv);
23extern void bus_remove_driver(struct device_driver *); 58extern void bus_remove_driver(struct device_driver *drv);
24 59
25extern void driver_detach(struct device_driver * drv); 60extern void driver_detach(struct device_driver *drv);
26extern int driver_probe_device(struct device_driver *, struct device *); 61extern int driver_probe_device(struct device_driver *drv, struct device *dev);
27 62
28extern void sysdev_shutdown(void); 63extern void sysdev_shutdown(void);
29extern int sysdev_suspend(pm_message_t state); 64extern int sysdev_suspend(pm_message_t state);
@@ -44,4 +79,13 @@ extern char *make_class_name(const char *name, struct kobject *kobj);
44 79
45extern int devres_release_all(struct device *dev); 80extern int devres_release_all(struct device *dev);
46 81
47extern struct kset devices_subsys; 82extern struct kset *devices_kset;
83
84#if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
85extern void module_add_driver(struct module *mod, struct device_driver *drv);
86extern void module_remove_driver(struct device_driver *drv);
87#else
88static inline void module_add_driver(struct module *mod,
89 struct device_driver *drv) { }
90static inline void module_remove_driver(struct device_driver *drv) { }
91#endif
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 9a19b071c573..f484495b2ad1 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -3,6 +3,8 @@
3 * 3 *
4 * Copyright (c) 2002-3 Patrick Mochel 4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs 5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc.
6 * 8 *
7 * This file is released under the GPLv2 9 * This file is released under the GPLv2
8 * 10 *
@@ -17,14 +19,13 @@
17#include "power/power.h" 19#include "power/power.h"
18 20
19#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 21#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
20#define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj) 22#define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj)
21 23
22/* 24/*
23 * sysfs bindings for drivers 25 * sysfs bindings for drivers
24 */ 26 */
25 27
26#define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) 28#define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
27#define to_driver(obj) container_of(obj, struct device_driver, kobj)
28 29
29 30
30static int __must_check bus_rescan_devices_helper(struct device *dev, 31static int __must_check bus_rescan_devices_helper(struct device *dev,
@@ -32,37 +33,40 @@ static int __must_check bus_rescan_devices_helper(struct device *dev,
32 33
33static struct bus_type *bus_get(struct bus_type *bus) 34static struct bus_type *bus_get(struct bus_type *bus)
34{ 35{
35 return bus ? container_of(kset_get(&bus->subsys), 36 if (bus) {
36 struct bus_type, subsys) : NULL; 37 kset_get(&bus->p->subsys);
38 return bus;
39 }
40 return NULL;
37} 41}
38 42
39static void bus_put(struct bus_type *bus) 43static void bus_put(struct bus_type *bus)
40{ 44{
41 kset_put(&bus->subsys); 45 if (bus)
46 kset_put(&bus->p->subsys);
42} 47}
43 48
44static ssize_t 49static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
45drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) 50 char *buf)
46{ 51{
47 struct driver_attribute * drv_attr = to_drv_attr(attr); 52 struct driver_attribute *drv_attr = to_drv_attr(attr);
48 struct device_driver * drv = to_driver(kobj); 53 struct driver_private *drv_priv = to_driver(kobj);
49 ssize_t ret = -EIO; 54 ssize_t ret = -EIO;
50 55
51 if (drv_attr->show) 56 if (drv_attr->show)
52 ret = drv_attr->show(drv, buf); 57 ret = drv_attr->show(drv_priv->driver, buf);
53 return ret; 58 return ret;
54} 59}
55 60
56static ssize_t 61static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
57drv_attr_store(struct kobject * kobj, struct attribute * attr, 62 const char *buf, size_t count)
58 const char * buf, size_t count)
59{ 63{
60 struct driver_attribute * drv_attr = to_drv_attr(attr); 64 struct driver_attribute *drv_attr = to_drv_attr(attr);
61 struct device_driver * drv = to_driver(kobj); 65 struct driver_private *drv_priv = to_driver(kobj);
62 ssize_t ret = -EIO; 66 ssize_t ret = -EIO;
63 67
64 if (drv_attr->store) 68 if (drv_attr->store)
65 ret = drv_attr->store(drv, buf, count); 69 ret = drv_attr->store(drv_priv->driver, buf, count);
66 return ret; 70 return ret;
67} 71}
68 72
@@ -71,22 +75,12 @@ static struct sysfs_ops driver_sysfs_ops = {
71 .store = drv_attr_store, 75 .store = drv_attr_store,
72}; 76};
73 77
74 78static void driver_release(struct kobject *kobj)
75static void driver_release(struct kobject * kobj)
76{ 79{
77 /* 80 struct driver_private *drv_priv = to_driver(kobj);
78 * Yes this is an empty release function, it is this way because struct 81
79 * device is always a static object, not a dynamic one. Yes, this is 82 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __FUNCTION__);
80 * not nice and bad, but remember, drivers are code, reference counted 83 kfree(drv_priv);
81 * by the module count, not a device, which is really data. And yes,
82 * in the future I do want to have all drivers be created dynamically,
83 * and am working toward that goal, but it will take a bit longer...
84 *
85 * But do not let this example give _anyone_ the idea that they can
86 * create a release function without any code in it at all, to do that
87 * is almost always wrong. If you have any questions about this,
88 * please send an email to <greg@kroah.com>
89 */
90} 84}
91 85
92static struct kobj_type driver_ktype = { 86static struct kobj_type driver_ktype = {
@@ -94,34 +88,30 @@ static struct kobj_type driver_ktype = {
94 .release = driver_release, 88 .release = driver_release,
95}; 89};
96 90
97
98/* 91/*
99 * sysfs bindings for buses 92 * sysfs bindings for buses
100 */ 93 */
101 94static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
102 95 char *buf)
103static ssize_t
104bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
105{ 96{
106 struct bus_attribute * bus_attr = to_bus_attr(attr); 97 struct bus_attribute *bus_attr = to_bus_attr(attr);
107 struct bus_type * bus = to_bus(kobj); 98 struct bus_type_private *bus_priv = to_bus(kobj);
108 ssize_t ret = 0; 99 ssize_t ret = 0;
109 100
110 if (bus_attr->show) 101 if (bus_attr->show)
111 ret = bus_attr->show(bus, buf); 102 ret = bus_attr->show(bus_priv->bus, buf);
112 return ret; 103 return ret;
113} 104}
114 105
115static ssize_t 106static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
116bus_attr_store(struct kobject * kobj, struct attribute * attr, 107 const char *buf, size_t count)
117 const char * buf, size_t count)
118{ 108{
119 struct bus_attribute * bus_attr = to_bus_attr(attr); 109 struct bus_attribute *bus_attr = to_bus_attr(attr);
120 struct bus_type * bus = to_bus(kobj); 110 struct bus_type_private *bus_priv = to_bus(kobj);
121 ssize_t ret = 0; 111 ssize_t ret = 0;
122 112
123 if (bus_attr->store) 113 if (bus_attr->store)
124 ret = bus_attr->store(bus, buf, count); 114 ret = bus_attr->store(bus_priv->bus, buf, count);
125 return ret; 115 return ret;
126} 116}
127 117
@@ -130,24 +120,26 @@ static struct sysfs_ops bus_sysfs_ops = {
130 .store = bus_attr_store, 120 .store = bus_attr_store,
131}; 121};
132 122
133int bus_create_file(struct bus_type * bus, struct bus_attribute * attr) 123int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
134{ 124{
135 int error; 125 int error;
136 if (bus_get(bus)) { 126 if (bus_get(bus)) {
137 error = sysfs_create_file(&bus->subsys.kobj, &attr->attr); 127 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
138 bus_put(bus); 128 bus_put(bus);
139 } else 129 } else
140 error = -EINVAL; 130 error = -EINVAL;
141 return error; 131 return error;
142} 132}
133EXPORT_SYMBOL_GPL(bus_create_file);
143 134
144void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr) 135void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
145{ 136{
146 if (bus_get(bus)) { 137 if (bus_get(bus)) {
147 sysfs_remove_file(&bus->subsys.kobj, &attr->attr); 138 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
148 bus_put(bus); 139 bus_put(bus);
149 } 140 }
150} 141}
142EXPORT_SYMBOL_GPL(bus_remove_file);
151 143
152static struct kobj_type bus_ktype = { 144static struct kobj_type bus_ktype = {
153 .sysfs_ops = &bus_sysfs_ops, 145 .sysfs_ops = &bus_sysfs_ops,
@@ -166,7 +158,7 @@ static struct kset_uevent_ops bus_uevent_ops = {
166 .filter = bus_uevent_filter, 158 .filter = bus_uevent_filter,
167}; 159};
168 160
169static decl_subsys(bus, &bus_ktype, &bus_uevent_ops); 161static struct kset *bus_kset;
170 162
171 163
172#ifdef CONFIG_HOTPLUG 164#ifdef CONFIG_HOTPLUG
@@ -224,10 +216,13 @@ static ssize_t driver_bind(struct device_driver *drv,
224 if (dev->parent) 216 if (dev->parent)
225 up(&dev->parent->sem); 217 up(&dev->parent->sem);
226 218
227 if (err > 0) /* success */ 219 if (err > 0) {
220 /* success */
228 err = count; 221 err = count;
229 else if (err == 0) /* driver didn't accept device */ 222 } else if (err == 0) {
223 /* driver didn't accept device */
230 err = -ENODEV; 224 err = -ENODEV;
225 }
231 } 226 }
232 put_device(dev); 227 put_device(dev);
233 bus_put(bus); 228 bus_put(bus);
@@ -237,16 +232,16 @@ static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
237 232
238static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf) 233static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
239{ 234{
240 return sprintf(buf, "%d\n", bus->drivers_autoprobe); 235 return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
241} 236}
242 237
243static ssize_t store_drivers_autoprobe(struct bus_type *bus, 238static ssize_t store_drivers_autoprobe(struct bus_type *bus,
244 const char *buf, size_t count) 239 const char *buf, size_t count)
245{ 240{
246 if (buf[0] == '0') 241 if (buf[0] == '0')
247 bus->drivers_autoprobe = 0; 242 bus->p->drivers_autoprobe = 0;
248 else 243 else
249 bus->drivers_autoprobe = 1; 244 bus->p->drivers_autoprobe = 1;
250 return count; 245 return count;
251} 246}
252 247
@@ -264,49 +259,49 @@ static ssize_t store_drivers_probe(struct bus_type *bus,
264} 259}
265#endif 260#endif
266 261
267static struct device * next_device(struct klist_iter * i) 262static struct device *next_device(struct klist_iter *i)
268{ 263{
269 struct klist_node * n = klist_next(i); 264 struct klist_node *n = klist_next(i);
270 return n ? container_of(n, struct device, knode_bus) : NULL; 265 return n ? container_of(n, struct device, knode_bus) : NULL;
271} 266}
272 267
273/** 268/**
274 * bus_for_each_dev - device iterator. 269 * bus_for_each_dev - device iterator.
275 * @bus: bus type. 270 * @bus: bus type.
276 * @start: device to start iterating from. 271 * @start: device to start iterating from.
277 * @data: data for the callback. 272 * @data: data for the callback.
278 * @fn: function to be called for each device. 273 * @fn: function to be called for each device.
279 * 274 *
280 * Iterate over @bus's list of devices, and call @fn for each, 275 * Iterate over @bus's list of devices, and call @fn for each,
281 * passing it @data. If @start is not NULL, we use that device to 276 * passing it @data. If @start is not NULL, we use that device to
282 * begin iterating from. 277 * begin iterating from.
283 * 278 *
284 * We check the return of @fn each time. If it returns anything 279 * We check the return of @fn each time. If it returns anything
285 * other than 0, we break out and return that value. 280 * other than 0, we break out and return that value.
286 * 281 *
287 * NOTE: The device that returns a non-zero value is not retained 282 * NOTE: The device that returns a non-zero value is not retained
288 * in any way, nor is its refcount incremented. If the caller needs 283 * in any way, nor is its refcount incremented. If the caller needs
289 * to retain this data, it should do, and increment the reference 284 * to retain this data, it should do, and increment the reference
290 * count in the supplied callback. 285 * count in the supplied callback.
291 */ 286 */
292 287int bus_for_each_dev(struct bus_type *bus, struct device *start,
293int bus_for_each_dev(struct bus_type * bus, struct device * start, 288 void *data, int (*fn)(struct device *, void *))
294 void * data, int (*fn)(struct device *, void *))
295{ 289{
296 struct klist_iter i; 290 struct klist_iter i;
297 struct device * dev; 291 struct device *dev;
298 int error = 0; 292 int error = 0;
299 293
300 if (!bus) 294 if (!bus)
301 return -EINVAL; 295 return -EINVAL;
302 296
303 klist_iter_init_node(&bus->klist_devices, &i, 297 klist_iter_init_node(&bus->p->klist_devices, &i,
304 (start ? &start->knode_bus : NULL)); 298 (start ? &start->knode_bus : NULL));
305 while ((dev = next_device(&i)) && !error) 299 while ((dev = next_device(&i)) && !error)
306 error = fn(dev, data); 300 error = fn(dev, data);
307 klist_iter_exit(&i); 301 klist_iter_exit(&i);
308 return error; 302 return error;
309} 303}
304EXPORT_SYMBOL_GPL(bus_for_each_dev);
310 305
311/** 306/**
312 * bus_find_device - device iterator for locating a particular device. 307 * bus_find_device - device iterator for locating a particular device.
@@ -323,9 +318,9 @@ int bus_for_each_dev(struct bus_type * bus, struct device * start,
323 * if it does. If the callback returns non-zero, this function will 318 * if it does. If the callback returns non-zero, this function will
324 * return to the caller and not iterate over any more devices. 319 * return to the caller and not iterate over any more devices.
325 */ 320 */
326struct device * bus_find_device(struct bus_type *bus, 321struct device *bus_find_device(struct bus_type *bus,
327 struct device *start, void *data, 322 struct device *start, void *data,
328 int (*match)(struct device *, void *)) 323 int (*match)(struct device *dev, void *data))
329{ 324{
330 struct klist_iter i; 325 struct klist_iter i;
331 struct device *dev; 326 struct device *dev;
@@ -333,7 +328,7 @@ struct device * bus_find_device(struct bus_type *bus,
333 if (!bus) 328 if (!bus)
334 return NULL; 329 return NULL;
335 330
336 klist_iter_init_node(&bus->klist_devices, &i, 331 klist_iter_init_node(&bus->p->klist_devices, &i,
337 (start ? &start->knode_bus : NULL)); 332 (start ? &start->knode_bus : NULL));
338 while ((dev = next_device(&i))) 333 while ((dev = next_device(&i)))
339 if (match(dev, data) && get_device(dev)) 334 if (match(dev, data) && get_device(dev))
@@ -341,51 +336,57 @@ struct device * bus_find_device(struct bus_type *bus,
341 klist_iter_exit(&i); 336 klist_iter_exit(&i);
342 return dev; 337 return dev;
343} 338}
339EXPORT_SYMBOL_GPL(bus_find_device);
344 340
345 341static struct device_driver *next_driver(struct klist_iter *i)
346static struct device_driver * next_driver(struct klist_iter * i)
347{ 342{
348 struct klist_node * n = klist_next(i); 343 struct klist_node *n = klist_next(i);
349 return n ? container_of(n, struct device_driver, knode_bus) : NULL; 344 struct driver_private *drv_priv;
345
346 if (n) {
347 drv_priv = container_of(n, struct driver_private, knode_bus);
348 return drv_priv->driver;
349 }
350 return NULL;
350} 351}
351 352
352/** 353/**
353 * bus_for_each_drv - driver iterator 354 * bus_for_each_drv - driver iterator
354 * @bus: bus we're dealing with. 355 * @bus: bus we're dealing with.
355 * @start: driver to start iterating on. 356 * @start: driver to start iterating on.
356 * @data: data to pass to the callback. 357 * @data: data to pass to the callback.
357 * @fn: function to call for each driver. 358 * @fn: function to call for each driver.
358 * 359 *
359 * This is nearly identical to the device iterator above. 360 * This is nearly identical to the device iterator above.
360 * We iterate over each driver that belongs to @bus, and call 361 * We iterate over each driver that belongs to @bus, and call
361 * @fn for each. If @fn returns anything but 0, we break out 362 * @fn for each. If @fn returns anything but 0, we break out
362 * and return it. If @start is not NULL, we use it as the head 363 * and return it. If @start is not NULL, we use it as the head
363 * of the list. 364 * of the list.
364 * 365 *
365 * NOTE: we don't return the driver that returns a non-zero 366 * NOTE: we don't return the driver that returns a non-zero
366 * value, nor do we leave the reference count incremented for that 367 * value, nor do we leave the reference count incremented for that
367 * driver. If the caller needs to know that info, it must set it 368 * driver. If the caller needs to know that info, it must set it
368 * in the callback. It must also be sure to increment the refcount 369 * in the callback. It must also be sure to increment the refcount
369 * so it doesn't disappear before returning to the caller. 370 * so it doesn't disappear before returning to the caller.
370 */ 371 */
371 372int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
372int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, 373 void *data, int (*fn)(struct device_driver *, void *))
373 void * data, int (*fn)(struct device_driver *, void *))
374{ 374{
375 struct klist_iter i; 375 struct klist_iter i;
376 struct device_driver * drv; 376 struct device_driver *drv;
377 int error = 0; 377 int error = 0;
378 378
379 if (!bus) 379 if (!bus)
380 return -EINVAL; 380 return -EINVAL;
381 381
382 klist_iter_init_node(&bus->klist_drivers, &i, 382 klist_iter_init_node(&bus->p->klist_drivers, &i,
383 start ? &start->knode_bus : NULL); 383 start ? &start->p->knode_bus : NULL);
384 while ((drv = next_driver(&i)) && !error) 384 while ((drv = next_driver(&i)) && !error)
385 error = fn(drv, data); 385 error = fn(drv, data);
386 klist_iter_exit(&i); 386 klist_iter_exit(&i);
387 return error; 387 return error;
388} 388}
389EXPORT_SYMBOL_GPL(bus_for_each_drv);
389 390
390static int device_add_attrs(struct bus_type *bus, struct device *dev) 391static int device_add_attrs(struct bus_type *bus, struct device *dev)
391{ 392{
@@ -396,7 +397,7 @@ static int device_add_attrs(struct bus_type *bus, struct device *dev)
396 return 0; 397 return 0;
397 398
398 for (i = 0; attr_name(bus->dev_attrs[i]); i++) { 399 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
399 error = device_create_file(dev,&bus->dev_attrs[i]); 400 error = device_create_file(dev, &bus->dev_attrs[i]);
400 if (error) { 401 if (error) {
401 while (--i >= 0) 402 while (--i >= 0)
402 device_remove_file(dev, &bus->dev_attrs[i]); 403 device_remove_file(dev, &bus->dev_attrs[i]);
@@ -406,13 +407,13 @@ static int device_add_attrs(struct bus_type *bus, struct device *dev)
406 return error; 407 return error;
407} 408}
408 409
409static void device_remove_attrs(struct bus_type * bus, struct device * dev) 410static void device_remove_attrs(struct bus_type *bus, struct device *dev)
410{ 411{
411 int i; 412 int i;
412 413
413 if (bus->dev_attrs) { 414 if (bus->dev_attrs) {
414 for (i = 0; attr_name(bus->dev_attrs[i]); i++) 415 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
415 device_remove_file(dev,&bus->dev_attrs[i]); 416 device_remove_file(dev, &bus->dev_attrs[i]);
416 } 417 }
417} 418}
418 419
@@ -420,7 +421,7 @@ static void device_remove_attrs(struct bus_type * bus, struct device * dev)
420static int make_deprecated_bus_links(struct device *dev) 421static int make_deprecated_bus_links(struct device *dev)
421{ 422{
422 return sysfs_create_link(&dev->kobj, 423 return sysfs_create_link(&dev->kobj,
423 &dev->bus->subsys.kobj, "bus"); 424 &dev->bus->p->subsys.kobj, "bus");
424} 425}
425 426
426static void remove_deprecated_bus_links(struct device *dev) 427static void remove_deprecated_bus_links(struct device *dev)
@@ -433,28 +434,28 @@ static inline void remove_deprecated_bus_links(struct device *dev) { }
433#endif 434#endif
434 435
435/** 436/**
436 * bus_add_device - add device to bus 437 * bus_add_device - add device to bus
437 * @dev: device being added 438 * @dev: device being added
438 * 439 *
439 * - Add the device to its bus's list of devices. 440 * - Add the device to its bus's list of devices.
440 * - Create link to device's bus. 441 * - Create link to device's bus.
441 */ 442 */
442int bus_add_device(struct device * dev) 443int bus_add_device(struct device *dev)
443{ 444{
444 struct bus_type * bus = bus_get(dev->bus); 445 struct bus_type *bus = bus_get(dev->bus);
445 int error = 0; 446 int error = 0;
446 447
447 if (bus) { 448 if (bus) {
448 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id); 449 pr_debug("bus: '%s': add device %s\n", bus->name, dev->bus_id);
449 error = device_add_attrs(bus, dev); 450 error = device_add_attrs(bus, dev);
450 if (error) 451 if (error)
451 goto out_put; 452 goto out_put;
452 error = sysfs_create_link(&bus->devices.kobj, 453 error = sysfs_create_link(&bus->p->devices_kset->kobj,
453 &dev->kobj, dev->bus_id); 454 &dev->kobj, dev->bus_id);
454 if (error) 455 if (error)
455 goto out_id; 456 goto out_id;
456 error = sysfs_create_link(&dev->kobj, 457 error = sysfs_create_link(&dev->kobj,
457 &dev->bus->subsys.kobj, "subsystem"); 458 &dev->bus->p->subsys.kobj, "subsystem");
458 if (error) 459 if (error)
459 goto out_subsys; 460 goto out_subsys;
460 error = make_deprecated_bus_links(dev); 461 error = make_deprecated_bus_links(dev);
@@ -466,7 +467,7 @@ int bus_add_device(struct device * dev)
466out_deprecated: 467out_deprecated:
467 sysfs_remove_link(&dev->kobj, "subsystem"); 468 sysfs_remove_link(&dev->kobj, "subsystem");
468out_subsys: 469out_subsys:
469 sysfs_remove_link(&bus->devices.kobj, dev->bus_id); 470 sysfs_remove_link(&bus->p->devices_kset->kobj, dev->bus_id);
470out_id: 471out_id:
471 device_remove_attrs(bus, dev); 472 device_remove_attrs(bus, dev);
472out_put: 473out_put:
@@ -475,56 +476,58 @@ out_put:
475} 476}
476 477
477/** 478/**
478 * bus_attach_device - add device to bus 479 * bus_attach_device - add device to bus
479 * @dev: device tried to attach to a driver 480 * @dev: device tried to attach to a driver
480 * 481 *
481 * - Add device to bus's list of devices. 482 * - Add device to bus's list of devices.
482 * - Try to attach to driver. 483 * - Try to attach to driver.
483 */ 484 */
484void bus_attach_device(struct device * dev) 485void bus_attach_device(struct device *dev)
485{ 486{
486 struct bus_type *bus = dev->bus; 487 struct bus_type *bus = dev->bus;
487 int ret = 0; 488 int ret = 0;
488 489
489 if (bus) { 490 if (bus) {
490 dev->is_registered = 1; 491 dev->is_registered = 1;
491 if (bus->drivers_autoprobe) 492 if (bus->p->drivers_autoprobe)
492 ret = device_attach(dev); 493 ret = device_attach(dev);
493 WARN_ON(ret < 0); 494 WARN_ON(ret < 0);
494 if (ret >= 0) 495 if (ret >= 0)
495 klist_add_tail(&dev->knode_bus, &bus->klist_devices); 496 klist_add_tail(&dev->knode_bus, &bus->p->klist_devices);
496 else 497 else
497 dev->is_registered = 0; 498 dev->is_registered = 0;
498 } 499 }
499} 500}
500 501
501/** 502/**
502 * bus_remove_device - remove device from bus 503 * bus_remove_device - remove device from bus
503 * @dev: device to be removed 504 * @dev: device to be removed
504 * 505 *
505 * - Remove symlink from bus's directory. 506 * - Remove symlink from bus's directory.
506 * - Delete device from bus's list. 507 * - Delete device from bus's list.
507 * - Detach from its driver. 508 * - Detach from its driver.
508 * - Drop reference taken in bus_add_device(). 509 * - Drop reference taken in bus_add_device().
509 */ 510 */
510void bus_remove_device(struct device * dev) 511void bus_remove_device(struct device *dev)
511{ 512{
512 if (dev->bus) { 513 if (dev->bus) {
513 sysfs_remove_link(&dev->kobj, "subsystem"); 514 sysfs_remove_link(&dev->kobj, "subsystem");
514 remove_deprecated_bus_links(dev); 515 remove_deprecated_bus_links(dev);
515 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id); 516 sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
517 dev->bus_id);
516 device_remove_attrs(dev->bus, dev); 518 device_remove_attrs(dev->bus, dev);
517 if (dev->is_registered) { 519 if (dev->is_registered) {
518 dev->is_registered = 0; 520 dev->is_registered = 0;
519 klist_del(&dev->knode_bus); 521 klist_del(&dev->knode_bus);
520 } 522 }
521 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id); 523 pr_debug("bus: '%s': remove device %s\n",
524 dev->bus->name, dev->bus_id);
522 device_release_driver(dev); 525 device_release_driver(dev);
523 bus_put(dev->bus); 526 bus_put(dev->bus);
524 } 527 }
525} 528}
526 529
527static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv) 530static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
528{ 531{
529 int error = 0; 532 int error = 0;
530 int i; 533 int i;
@@ -533,19 +536,19 @@ static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
533 for (i = 0; attr_name(bus->drv_attrs[i]); i++) { 536 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
534 error = driver_create_file(drv, &bus->drv_attrs[i]); 537 error = driver_create_file(drv, &bus->drv_attrs[i]);
535 if (error) 538 if (error)
536 goto Err; 539 goto err;
537 } 540 }
538 } 541 }
539 Done: 542done:
540 return error; 543 return error;
541 Err: 544err:
542 while (--i >= 0) 545 while (--i >= 0)
543 driver_remove_file(drv, &bus->drv_attrs[i]); 546 driver_remove_file(drv, &bus->drv_attrs[i]);
544 goto Done; 547 goto done;
545} 548}
546 549
547 550static void driver_remove_attrs(struct bus_type *bus,
548static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv) 551 struct device_driver *drv)
549{ 552{
550 int i; 553 int i;
551 554
@@ -616,39 +619,46 @@ static ssize_t driver_uevent_store(struct device_driver *drv,
616 enum kobject_action action; 619 enum kobject_action action;
617 620
618 if (kobject_action_type(buf, count, &action) == 0) 621 if (kobject_action_type(buf, count, &action) == 0)
619 kobject_uevent(&drv->kobj, action); 622 kobject_uevent(&drv->p->kobj, action);
620 return count; 623 return count;
621} 624}
622static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store); 625static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
623 626
624/** 627/**
625 * bus_add_driver - Add a driver to the bus. 628 * bus_add_driver - Add a driver to the bus.
626 * @drv: driver. 629 * @drv: driver.
627 *
628 */ 630 */
629int bus_add_driver(struct device_driver *drv) 631int bus_add_driver(struct device_driver *drv)
630{ 632{
631 struct bus_type * bus = bus_get(drv->bus); 633 struct bus_type *bus;
634 struct driver_private *priv;
632 int error = 0; 635 int error = 0;
633 636
637 bus = bus_get(drv->bus);
634 if (!bus) 638 if (!bus)
635 return -EINVAL; 639 return -EINVAL;
636 640
637 pr_debug("bus %s: add driver %s\n", bus->name, drv->name); 641 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
638 error = kobject_set_name(&drv->kobj, "%s", drv->name); 642
639 if (error) 643 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
640 goto out_put_bus; 644 if (!priv)
641 drv->kobj.kset = &bus->drivers; 645 return -ENOMEM;
642 error = kobject_register(&drv->kobj); 646
647 klist_init(&priv->klist_devices, NULL, NULL);
648 priv->driver = drv;
649 drv->p = priv;
650 priv->kobj.kset = bus->p->drivers_kset;
651 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
652 "%s", drv->name);
643 if (error) 653 if (error)
644 goto out_put_bus; 654 goto out_put_bus;
645 655
646 if (drv->bus->drivers_autoprobe) { 656 if (drv->bus->p->drivers_autoprobe) {
647 error = driver_attach(drv); 657 error = driver_attach(drv);
648 if (error) 658 if (error)
649 goto out_unregister; 659 goto out_unregister;
650 } 660 }
651 klist_add_tail(&drv->knode_bus, &bus->klist_drivers); 661 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
652 module_add_driver(drv->owner, drv); 662 module_add_driver(drv->owner, drv);
653 663
654 error = driver_create_file(drv, &driver_attr_uevent); 664 error = driver_create_file(drv, &driver_attr_uevent);
@@ -669,24 +679,24 @@ int bus_add_driver(struct device_driver *drv)
669 __FUNCTION__, drv->name); 679 __FUNCTION__, drv->name);
670 } 680 }
671 681
682 kobject_uevent(&priv->kobj, KOBJ_ADD);
672 return error; 683 return error;
673out_unregister: 684out_unregister:
674 kobject_unregister(&drv->kobj); 685 kobject_put(&priv->kobj);
675out_put_bus: 686out_put_bus:
676 bus_put(bus); 687 bus_put(bus);
677 return error; 688 return error;
678} 689}
679 690
680/** 691/**
681 * bus_remove_driver - delete driver from bus's knowledge. 692 * bus_remove_driver - delete driver from bus's knowledge.
682 * @drv: driver. 693 * @drv: driver.
683 * 694 *
684 * Detach the driver from the devices it controls, and remove 695 * Detach the driver from the devices it controls, and remove
685 * it from its bus's list of drivers. Finally, we drop the reference 696 * it from its bus's list of drivers. Finally, we drop the reference
686 * to the bus we took in bus_add_driver(). 697 * to the bus we took in bus_add_driver().
687 */ 698 */
688 699void bus_remove_driver(struct device_driver *drv)
689void bus_remove_driver(struct device_driver * drv)
690{ 700{
691 if (!drv->bus) 701 if (!drv->bus)
692 return; 702 return;
@@ -694,18 +704,17 @@ void bus_remove_driver(struct device_driver * drv)
694 remove_bind_files(drv); 704 remove_bind_files(drv);
695 driver_remove_attrs(drv->bus, drv); 705 driver_remove_attrs(drv->bus, drv);
696 driver_remove_file(drv, &driver_attr_uevent); 706 driver_remove_file(drv, &driver_attr_uevent);
697 klist_remove(&drv->knode_bus); 707 klist_remove(&drv->p->knode_bus);
698 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name); 708 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
699 driver_detach(drv); 709 driver_detach(drv);
700 module_remove_driver(drv); 710 module_remove_driver(drv);
701 kobject_unregister(&drv->kobj); 711 kobject_put(&drv->p->kobj);
702 bus_put(drv->bus); 712 bus_put(drv->bus);
703} 713}
704 714
705
706/* Helper for bus_rescan_devices's iter */ 715/* Helper for bus_rescan_devices's iter */
707static int __must_check bus_rescan_devices_helper(struct device *dev, 716static int __must_check bus_rescan_devices_helper(struct device *dev,
708 void *data) 717 void *data)
709{ 718{
710 int ret = 0; 719 int ret = 0;
711 720
@@ -727,10 +736,11 @@ static int __must_check bus_rescan_devices_helper(struct device *dev,
727 * attached and rescan it against existing drivers to see if it matches 736 * attached and rescan it against existing drivers to see if it matches
728 * any by calling device_attach() for the unbound devices. 737 * any by calling device_attach() for the unbound devices.
729 */ 738 */
730int bus_rescan_devices(struct bus_type * bus) 739int bus_rescan_devices(struct bus_type *bus)
731{ 740{
732 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 741 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
733} 742}
743EXPORT_SYMBOL_GPL(bus_rescan_devices);
734 744
735/** 745/**
736 * device_reprobe - remove driver for a device and probe for a new driver 746 * device_reprobe - remove driver for a device and probe for a new driver
@@ -755,55 +765,55 @@ int device_reprobe(struct device *dev)
755EXPORT_SYMBOL_GPL(device_reprobe); 765EXPORT_SYMBOL_GPL(device_reprobe);
756 766
757/** 767/**
758 * find_bus - locate bus by name. 768 * find_bus - locate bus by name.
759 * @name: name of bus. 769 * @name: name of bus.
760 * 770 *
761 * Call kset_find_obj() to iterate over list of buses to 771 * Call kset_find_obj() to iterate over list of buses to
762 * find a bus by name. Return bus if found. 772 * find a bus by name. Return bus if found.
763 * 773 *
764 * Note that kset_find_obj increments bus' reference count. 774 * Note that kset_find_obj increments bus' reference count.
765 */ 775 */
766#if 0 776#if 0
767struct bus_type * find_bus(char * name) 777struct bus_type *find_bus(char *name)
768{ 778{
769 struct kobject * k = kset_find_obj(&bus_subsys.kset, name); 779 struct kobject *k = kset_find_obj(bus_kset, name);
770 return k ? to_bus(k) : NULL; 780 return k ? to_bus(k) : NULL;
771} 781}
772#endif /* 0 */ 782#endif /* 0 */
773 783
774 784
775/** 785/**
776 * bus_add_attrs - Add default attributes for this bus. 786 * bus_add_attrs - Add default attributes for this bus.
777 * @bus: Bus that has just been registered. 787 * @bus: Bus that has just been registered.
778 */ 788 */
779 789
780static int bus_add_attrs(struct bus_type * bus) 790static int bus_add_attrs(struct bus_type *bus)
781{ 791{
782 int error = 0; 792 int error = 0;
783 int i; 793 int i;
784 794
785 if (bus->bus_attrs) { 795 if (bus->bus_attrs) {
786 for (i = 0; attr_name(bus->bus_attrs[i]); i++) { 796 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
787 error = bus_create_file(bus,&bus->bus_attrs[i]); 797 error = bus_create_file(bus, &bus->bus_attrs[i]);
788 if (error) 798 if (error)
789 goto Err; 799 goto err;
790 } 800 }
791 } 801 }
792 Done: 802done:
793 return error; 803 return error;
794 Err: 804err:
795 while (--i >= 0) 805 while (--i >= 0)
796 bus_remove_file(bus,&bus->bus_attrs[i]); 806 bus_remove_file(bus, &bus->bus_attrs[i]);
797 goto Done; 807 goto done;
798} 808}
799 809
800static void bus_remove_attrs(struct bus_type * bus) 810static void bus_remove_attrs(struct bus_type *bus)
801{ 811{
802 int i; 812 int i;
803 813
804 if (bus->bus_attrs) { 814 if (bus->bus_attrs) {
805 for (i = 0; attr_name(bus->bus_attrs[i]); i++) 815 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
806 bus_remove_file(bus,&bus->bus_attrs[i]); 816 bus_remove_file(bus, &bus->bus_attrs[i]);
807 } 817 }
808} 818}
809 819
@@ -827,32 +837,42 @@ static ssize_t bus_uevent_store(struct bus_type *bus,
827 enum kobject_action action; 837 enum kobject_action action;
828 838
829 if (kobject_action_type(buf, count, &action) == 0) 839 if (kobject_action_type(buf, count, &action) == 0)
830 kobject_uevent(&bus->subsys.kobj, action); 840 kobject_uevent(&bus->p->subsys.kobj, action);
831 return count; 841 return count;
832} 842}
833static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store); 843static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
834 844
835/** 845/**
836 * bus_register - register a bus with the system. 846 * bus_register - register a bus with the system.
837 * @bus: bus. 847 * @bus: bus.
838 * 848 *
839 * Once we have that, we registered the bus with the kobject 849 * Once we have that, we registered the bus with the kobject
840 * infrastructure, then register the children subsystems it has: 850 * infrastructure, then register the children subsystems it has:
841 * the devices and drivers that belong to the bus. 851 * the devices and drivers that belong to the bus.
842 */ 852 */
843int bus_register(struct bus_type * bus) 853int bus_register(struct bus_type *bus)
844{ 854{
845 int retval; 855 int retval;
856 struct bus_type_private *priv;
857
858 priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
859 if (!priv)
860 return -ENOMEM;
846 861
847 BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier); 862 priv->bus = bus;
863 bus->p = priv;
848 864
849 retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name); 865 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
866
867 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
850 if (retval) 868 if (retval)
851 goto out; 869 goto out;
852 870
853 bus->subsys.kobj.kset = &bus_subsys; 871 priv->subsys.kobj.kset = bus_kset;
872 priv->subsys.kobj.ktype = &bus_ktype;
873 priv->drivers_autoprobe = 1;
854 874
855 retval = subsystem_register(&bus->subsys); 875 retval = kset_register(&priv->subsys);
856 if (retval) 876 if (retval)
857 goto out; 877 goto out;
858 878
@@ -860,23 +880,23 @@ int bus_register(struct bus_type * bus)
860 if (retval) 880 if (retval)
861 goto bus_uevent_fail; 881 goto bus_uevent_fail;
862 882
863 kobject_set_name(&bus->devices.kobj, "devices"); 883 priv->devices_kset = kset_create_and_add("devices", NULL,
864 bus->devices.kobj.parent = &bus->subsys.kobj; 884 &priv->subsys.kobj);
865 retval = kset_register(&bus->devices); 885 if (!priv->devices_kset) {
866 if (retval) 886 retval = -ENOMEM;
867 goto bus_devices_fail; 887 goto bus_devices_fail;
888 }
868 889
869 kobject_set_name(&bus->drivers.kobj, "drivers"); 890 priv->drivers_kset = kset_create_and_add("drivers", NULL,
870 bus->drivers.kobj.parent = &bus->subsys.kobj; 891 &priv->subsys.kobj);
871 bus->drivers.ktype = &driver_ktype; 892 if (!priv->drivers_kset) {
872 retval = kset_register(&bus->drivers); 893 retval = -ENOMEM;
873 if (retval)
874 goto bus_drivers_fail; 894 goto bus_drivers_fail;
895 }
875 896
876 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put); 897 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
877 klist_init(&bus->klist_drivers, NULL, NULL); 898 klist_init(&priv->klist_drivers, NULL, NULL);
878 899
879 bus->drivers_autoprobe = 1;
880 retval = add_probe_files(bus); 900 retval = add_probe_files(bus);
881 if (retval) 901 if (retval)
882 goto bus_probe_files_fail; 902 goto bus_probe_files_fail;
@@ -885,66 +905,73 @@ int bus_register(struct bus_type * bus)
885 if (retval) 905 if (retval)
886 goto bus_attrs_fail; 906 goto bus_attrs_fail;
887 907
888 pr_debug("bus type '%s' registered\n", bus->name); 908 pr_debug("bus: '%s': registered\n", bus->name);
889 return 0; 909 return 0;
890 910
891bus_attrs_fail: 911bus_attrs_fail:
892 remove_probe_files(bus); 912 remove_probe_files(bus);
893bus_probe_files_fail: 913bus_probe_files_fail:
894 kset_unregister(&bus->drivers); 914 kset_unregister(bus->p->drivers_kset);
895bus_drivers_fail: 915bus_drivers_fail:
896 kset_unregister(&bus->devices); 916 kset_unregister(bus->p->devices_kset);
897bus_devices_fail: 917bus_devices_fail:
898 bus_remove_file(bus, &bus_attr_uevent); 918 bus_remove_file(bus, &bus_attr_uevent);
899bus_uevent_fail: 919bus_uevent_fail:
900 subsystem_unregister(&bus->subsys); 920 kset_unregister(&bus->p->subsys);
921 kfree(bus->p);
901out: 922out:
902 return retval; 923 return retval;
903} 924}
925EXPORT_SYMBOL_GPL(bus_register);
904 926
905/** 927/**
906 * bus_unregister - remove a bus from the system 928 * bus_unregister - remove a bus from the system
907 * @bus: bus. 929 * @bus: bus.
908 * 930 *
909 * Unregister the child subsystems and the bus itself. 931 * Unregister the child subsystems and the bus itself.
910 * Finally, we call bus_put() to release the refcount 932 * Finally, we call bus_put() to release the refcount
911 */ 933 */
912void bus_unregister(struct bus_type * bus) 934void bus_unregister(struct bus_type *bus)
913{ 935{
914 pr_debug("bus %s: unregistering\n", bus->name); 936 pr_debug("bus: '%s': unregistering\n", bus->name);
915 bus_remove_attrs(bus); 937 bus_remove_attrs(bus);
916 remove_probe_files(bus); 938 remove_probe_files(bus);
917 kset_unregister(&bus->drivers); 939 kset_unregister(bus->p->drivers_kset);
918 kset_unregister(&bus->devices); 940 kset_unregister(bus->p->devices_kset);
919 bus_remove_file(bus, &bus_attr_uevent); 941 bus_remove_file(bus, &bus_attr_uevent);
920 subsystem_unregister(&bus->subsys); 942 kset_unregister(&bus->p->subsys);
943 kfree(bus->p);
921} 944}
945EXPORT_SYMBOL_GPL(bus_unregister);
922 946
923int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) 947int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
924{ 948{
925 return blocking_notifier_chain_register(&bus->bus_notifier, nb); 949 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
926} 950}
927EXPORT_SYMBOL_GPL(bus_register_notifier); 951EXPORT_SYMBOL_GPL(bus_register_notifier);
928 952
929int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) 953int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
930{ 954{
931 return blocking_notifier_chain_unregister(&bus->bus_notifier, nb); 955 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
932} 956}
933EXPORT_SYMBOL_GPL(bus_unregister_notifier); 957EXPORT_SYMBOL_GPL(bus_unregister_notifier);
934 958
935int __init buses_init(void) 959struct kset *bus_get_kset(struct bus_type *bus)
936{ 960{
937 return subsystem_register(&bus_subsys); 961 return &bus->p->subsys;
938} 962}
963EXPORT_SYMBOL_GPL(bus_get_kset);
939 964
965struct klist *bus_get_device_klist(struct bus_type *bus)
966{
967 return &bus->p->klist_devices;
968}
969EXPORT_SYMBOL_GPL(bus_get_device_klist);
940 970
941EXPORT_SYMBOL_GPL(bus_for_each_dev); 971int __init buses_init(void)
942EXPORT_SYMBOL_GPL(bus_find_device); 972{
943EXPORT_SYMBOL_GPL(bus_for_each_drv); 973 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
944 974 if (!bus_kset)
945EXPORT_SYMBOL_GPL(bus_register); 975 return -ENOMEM;
946EXPORT_SYMBOL_GPL(bus_unregister); 976 return 0;
947EXPORT_SYMBOL_GPL(bus_rescan_devices); 977}
948
949EXPORT_SYMBOL_GPL(bus_create_file);
950EXPORT_SYMBOL_GPL(bus_remove_file);
diff --git a/drivers/base/class.c b/drivers/base/class.c
index a863bb091e11..59cf35894cfc 100644
--- a/drivers/base/class.c
+++ b/drivers/base/class.c
@@ -17,16 +17,17 @@
17#include <linux/kdev_t.h> 17#include <linux/kdev_t.h>
18#include <linux/err.h> 18#include <linux/err.h>
19#include <linux/slab.h> 19#include <linux/slab.h>
20#include <linux/genhd.h>
20#include "base.h" 21#include "base.h"
21 22
22#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) 23#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
23#define to_class(obj) container_of(obj, struct class, subsys.kobj) 24#define to_class(obj) container_of(obj, struct class, subsys.kobj)
24 25
25static ssize_t 26static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
26class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) 27 char *buf)
27{ 28{
28 struct class_attribute * class_attr = to_class_attr(attr); 29 struct class_attribute *class_attr = to_class_attr(attr);
29 struct class * dc = to_class(kobj); 30 struct class *dc = to_class(kobj);
30 ssize_t ret = -EIO; 31 ssize_t ret = -EIO;
31 32
32 if (class_attr->show) 33 if (class_attr->show)
@@ -34,12 +35,11 @@ class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
34 return ret; 35 return ret;
35} 36}
36 37
37static ssize_t 38static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
38class_attr_store(struct kobject * kobj, struct attribute * attr, 39 const char *buf, size_t count)
39 const char * buf, size_t count)
40{ 40{
41 struct class_attribute * class_attr = to_class_attr(attr); 41 struct class_attribute *class_attr = to_class_attr(attr);
42 struct class * dc = to_class(kobj); 42 struct class *dc = to_class(kobj);
43 ssize_t ret = -EIO; 43 ssize_t ret = -EIO;
44 44
45 if (class_attr->store) 45 if (class_attr->store)
@@ -47,7 +47,7 @@ class_attr_store(struct kobject * kobj, struct attribute * attr,
47 return ret; 47 return ret;
48} 48}
49 49
50static void class_release(struct kobject * kobj) 50static void class_release(struct kobject *kobj)
51{ 51{
52 struct class *class = to_class(kobj); 52 struct class *class = to_class(kobj);
53 53
@@ -71,20 +71,20 @@ static struct kobj_type class_ktype = {
71}; 71};
72 72
73/* Hotplug events for classes go to the class_obj subsys */ 73/* Hotplug events for classes go to the class_obj subsys */
74static decl_subsys(class, &class_ktype, NULL); 74static struct kset *class_kset;
75 75
76 76
77int class_create_file(struct class * cls, const struct class_attribute * attr) 77int class_create_file(struct class *cls, const struct class_attribute *attr)
78{ 78{
79 int error; 79 int error;
80 if (cls) { 80 if (cls)
81 error = sysfs_create_file(&cls->subsys.kobj, &attr->attr); 81 error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
82 } else 82 else
83 error = -EINVAL; 83 error = -EINVAL;
84 return error; 84 return error;
85} 85}
86 86
87void class_remove_file(struct class * cls, const struct class_attribute * attr) 87void class_remove_file(struct class *cls, const struct class_attribute *attr)
88{ 88{
89 if (cls) 89 if (cls)
90 sysfs_remove_file(&cls->subsys.kobj, &attr->attr); 90 sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
@@ -93,48 +93,48 @@ void class_remove_file(struct class * cls, const struct class_attribute * attr)
93static struct class *class_get(struct class *cls) 93static struct class *class_get(struct class *cls)
94{ 94{
95 if (cls) 95 if (cls)
96 return container_of(kset_get(&cls->subsys), struct class, subsys); 96 return container_of(kset_get(&cls->subsys),
97 struct class, subsys);
97 return NULL; 98 return NULL;
98} 99}
99 100
100static void class_put(struct class * cls) 101static void class_put(struct class *cls)
101{ 102{
102 if (cls) 103 if (cls)
103 kset_put(&cls->subsys); 104 kset_put(&cls->subsys);
104} 105}
105 106
106 107static int add_class_attrs(struct class *cls)
107static int add_class_attrs(struct class * cls)
108{ 108{
109 int i; 109 int i;
110 int error = 0; 110 int error = 0;
111 111
112 if (cls->class_attrs) { 112 if (cls->class_attrs) {
113 for (i = 0; attr_name(cls->class_attrs[i]); i++) { 113 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
114 error = class_create_file(cls,&cls->class_attrs[i]); 114 error = class_create_file(cls, &cls->class_attrs[i]);
115 if (error) 115 if (error)
116 goto Err; 116 goto error;
117 } 117 }
118 } 118 }
119 Done: 119done:
120 return error; 120 return error;
121 Err: 121error:
122 while (--i >= 0) 122 while (--i >= 0)
123 class_remove_file(cls,&cls->class_attrs[i]); 123 class_remove_file(cls, &cls->class_attrs[i]);
124 goto Done; 124 goto done;
125} 125}
126 126
127static void remove_class_attrs(struct class * cls) 127static void remove_class_attrs(struct class *cls)
128{ 128{
129 int i; 129 int i;
130 130
131 if (cls->class_attrs) { 131 if (cls->class_attrs) {
132 for (i = 0; attr_name(cls->class_attrs[i]); i++) 132 for (i = 0; attr_name(cls->class_attrs[i]); i++)
133 class_remove_file(cls,&cls->class_attrs[i]); 133 class_remove_file(cls, &cls->class_attrs[i]);
134 } 134 }
135} 135}
136 136
137int class_register(struct class * cls) 137int class_register(struct class *cls)
138{ 138{
139 int error; 139 int error;
140 140
@@ -149,9 +149,16 @@ int class_register(struct class * cls)
149 if (error) 149 if (error)
150 return error; 150 return error;
151 151
152 cls->subsys.kobj.kset = &class_subsys; 152#ifdef CONFIG_SYSFS_DEPRECATED
153 /* let the block class directory show up in the root of sysfs */
154 if (cls != &block_class)
155 cls->subsys.kobj.kset = class_kset;
156#else
157 cls->subsys.kobj.kset = class_kset;
158#endif
159 cls->subsys.kobj.ktype = &class_ktype;
153 160
154 error = subsystem_register(&cls->subsys); 161 error = kset_register(&cls->subsys);
155 if (!error) { 162 if (!error) {
156 error = add_class_attrs(class_get(cls)); 163 error = add_class_attrs(class_get(cls));
157 class_put(cls); 164 class_put(cls);
@@ -159,11 +166,11 @@ int class_register(struct class * cls)
159 return error; 166 return error;
160} 167}
161 168
162void class_unregister(struct class * cls) 169void class_unregister(struct class *cls)
163{ 170{
164 pr_debug("device class '%s': unregistering\n", cls->name); 171 pr_debug("device class '%s': unregistering\n", cls->name);
165 remove_class_attrs(cls); 172 remove_class_attrs(cls);
166 subsystem_unregister(&cls->subsys); 173 kset_unregister(&cls->subsys);
167} 174}
168 175
169static void class_create_release(struct class *cls) 176static void class_create_release(struct class *cls)
@@ -241,8 +248,8 @@ void class_destroy(struct class *cls)
241 248
242/* Class Device Stuff */ 249/* Class Device Stuff */
243 250
244int class_device_create_file(struct class_device * class_dev, 251int class_device_create_file(struct class_device *class_dev,
245 const struct class_device_attribute * attr) 252 const struct class_device_attribute *attr)
246{ 253{
247 int error = -EINVAL; 254 int error = -EINVAL;
248 if (class_dev) 255 if (class_dev)
@@ -250,8 +257,8 @@ int class_device_create_file(struct class_device * class_dev,
250 return error; 257 return error;
251} 258}
252 259
253void class_device_remove_file(struct class_device * class_dev, 260void class_device_remove_file(struct class_device *class_dev,
254 const struct class_device_attribute * attr) 261 const struct class_device_attribute *attr)
255{ 262{
256 if (class_dev) 263 if (class_dev)
257 sysfs_remove_file(&class_dev->kobj, &attr->attr); 264 sysfs_remove_file(&class_dev->kobj, &attr->attr);
@@ -273,12 +280,11 @@ void class_device_remove_bin_file(struct class_device *class_dev,
273 sysfs_remove_bin_file(&class_dev->kobj, attr); 280 sysfs_remove_bin_file(&class_dev->kobj, attr);
274} 281}
275 282
276static ssize_t 283static ssize_t class_device_attr_show(struct kobject *kobj,
277class_device_attr_show(struct kobject * kobj, struct attribute * attr, 284 struct attribute *attr, char *buf)
278 char * buf)
279{ 285{
280 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); 286 struct class_device_attribute *class_dev_attr = to_class_dev_attr(attr);
281 struct class_device * cd = to_class_dev(kobj); 287 struct class_device *cd = to_class_dev(kobj);
282 ssize_t ret = 0; 288 ssize_t ret = 0;
283 289
284 if (class_dev_attr->show) 290 if (class_dev_attr->show)
@@ -286,12 +292,12 @@ class_device_attr_show(struct kobject * kobj, struct attribute * attr,
286 return ret; 292 return ret;
287} 293}
288 294
289static ssize_t 295static ssize_t class_device_attr_store(struct kobject *kobj,
290class_device_attr_store(struct kobject * kobj, struct attribute * attr, 296 struct attribute *attr,
291 const char * buf, size_t count) 297 const char *buf, size_t count)
292{ 298{
293 struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr); 299 struct class_device_attribute *class_dev_attr = to_class_dev_attr(attr);
294 struct class_device * cd = to_class_dev(kobj); 300 struct class_device *cd = to_class_dev(kobj);
295 ssize_t ret = 0; 301 ssize_t ret = 0;
296 302
297 if (class_dev_attr->store) 303 if (class_dev_attr->store)
@@ -304,10 +310,10 @@ static struct sysfs_ops class_dev_sysfs_ops = {
304 .store = class_device_attr_store, 310 .store = class_device_attr_store,
305}; 311};
306 312
307static void class_dev_release(struct kobject * kobj) 313static void class_dev_release(struct kobject *kobj)
308{ 314{
309 struct class_device *cd = to_class_dev(kobj); 315 struct class_device *cd = to_class_dev(kobj);
310 struct class * cls = cd->class; 316 struct class *cls = cd->class;
311 317
312 pr_debug("device class '%s': release.\n", cd->class_id); 318 pr_debug("device class '%s': release.\n", cd->class_id);
313 319
@@ -316,8 +322,8 @@ static void class_dev_release(struct kobject * kobj)
316 else if (cls->release) 322 else if (cls->release)
317 cls->release(cd); 323 cls->release(cd);
318 else { 324 else {
319 printk(KERN_ERR "Class Device '%s' does not have a release() function, " 325 printk(KERN_ERR "Class Device '%s' does not have a release() "
320 "it is broken and must be fixed.\n", 326 "function, it is broken and must be fixed.\n",
321 cd->class_id); 327 cd->class_id);
322 WARN_ON(1); 328 WARN_ON(1);
323 } 329 }
@@ -428,7 +434,8 @@ static int class_uevent(struct kset *kset, struct kobject *kobj,
428 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); 434 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
429 435
430 if (dev->driver) 436 if (dev->driver)
431 add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name); 437 add_uevent_var(env, "PHYSDEVDRIVER=%s",
438 dev->driver->name);
432 } 439 }
433 440
434 if (class_dev->uevent) { 441 if (class_dev->uevent) {
@@ -452,43 +459,49 @@ static struct kset_uevent_ops class_uevent_ops = {
452 .uevent = class_uevent, 459 .uevent = class_uevent,
453}; 460};
454 461
455static decl_subsys(class_obj, &class_device_ktype, &class_uevent_ops); 462/*
456 463 * DO NOT copy how this is created, kset_create_and_add() should be
464 * called, but this is a hold-over from the old-way and will be deleted
465 * entirely soon.
466 */
467static struct kset class_obj_subsys = {
468 .uevent_ops = &class_uevent_ops,
469};
457 470
458static int class_device_add_attrs(struct class_device * cd) 471static int class_device_add_attrs(struct class_device *cd)
459{ 472{
460 int i; 473 int i;
461 int error = 0; 474 int error = 0;
462 struct class * cls = cd->class; 475 struct class *cls = cd->class;
463 476
464 if (cls->class_dev_attrs) { 477 if (cls->class_dev_attrs) {
465 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) { 478 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
466 error = class_device_create_file(cd, 479 error = class_device_create_file(cd,
467 &cls->class_dev_attrs[i]); 480 &cls->class_dev_attrs[i]);
468 if (error) 481 if (error)
469 goto Err; 482 goto err;
470 } 483 }
471 } 484 }
472 Done: 485done:
473 return error; 486 return error;
474 Err: 487err:
475 while (--i >= 0) 488 while (--i >= 0)
476 class_device_remove_file(cd,&cls->class_dev_attrs[i]); 489 class_device_remove_file(cd, &cls->class_dev_attrs[i]);
477 goto Done; 490 goto done;
478} 491}
479 492
480static void class_device_remove_attrs(struct class_device * cd) 493static void class_device_remove_attrs(struct class_device *cd)
481{ 494{
482 int i; 495 int i;
483 struct class * cls = cd->class; 496 struct class *cls = cd->class;
484 497
485 if (cls->class_dev_attrs) { 498 if (cls->class_dev_attrs) {
486 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) 499 for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
487 class_device_remove_file(cd,&cls->class_dev_attrs[i]); 500 class_device_remove_file(cd, &cls->class_dev_attrs[i]);
488 } 501 }
489} 502}
490 503
491static int class_device_add_groups(struct class_device * cd) 504static int class_device_add_groups(struct class_device *cd)
492{ 505{
493 int i; 506 int i;
494 int error = 0; 507 int error = 0;
@@ -498,7 +511,8 @@ static int class_device_add_groups(struct class_device * cd)
498 error = sysfs_create_group(&cd->kobj, cd->groups[i]); 511 error = sysfs_create_group(&cd->kobj, cd->groups[i]);
499 if (error) { 512 if (error) {
500 while (--i >= 0) 513 while (--i >= 0)
501 sysfs_remove_group(&cd->kobj, cd->groups[i]); 514 sysfs_remove_group(&cd->kobj,
515 cd->groups[i]);
502 goto out; 516 goto out;
503 } 517 }
504 } 518 }
@@ -507,14 +521,12 @@ out:
507 return error; 521 return error;
508} 522}
509 523
510static void class_device_remove_groups(struct class_device * cd) 524static void class_device_remove_groups(struct class_device *cd)
511{ 525{
512 int i; 526 int i;
513 if (cd->groups) { 527 if (cd->groups)
514 for (i = 0; cd->groups[i]; i++) { 528 for (i = 0; cd->groups[i]; i++)
515 sysfs_remove_group(&cd->kobj, cd->groups[i]); 529 sysfs_remove_group(&cd->kobj, cd->groups[i]);
516 }
517 }
518} 530}
519 531
520static ssize_t show_dev(struct class_device *class_dev, char *buf) 532static ssize_t show_dev(struct class_device *class_dev, char *buf)
@@ -537,8 +549,8 @@ static struct class_device_attribute class_uevent_attr =
537 549
538void class_device_initialize(struct class_device *class_dev) 550void class_device_initialize(struct class_device *class_dev)
539{ 551{
540 kobj_set_kset_s(class_dev, class_obj_subsys); 552 class_dev->kobj.kset = &class_obj_subsys;
541 kobject_init(&class_dev->kobj); 553 kobject_init(&class_dev->kobj, &class_device_ktype);
542 INIT_LIST_HEAD(&class_dev->node); 554 INIT_LIST_HEAD(&class_dev->node);
543} 555}
544 556
@@ -566,16 +578,13 @@ int class_device_add(struct class_device *class_dev)
566 class_dev->class_id); 578 class_dev->class_id);
567 579
568 /* first, register with generic layer. */ 580 /* first, register with generic layer. */
569 error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
570 if (error)
571 goto out2;
572
573 if (parent_class_dev) 581 if (parent_class_dev)
574 class_dev->kobj.parent = &parent_class_dev->kobj; 582 class_dev->kobj.parent = &parent_class_dev->kobj;
575 else 583 else
576 class_dev->kobj.parent = &parent_class->subsys.kobj; 584 class_dev->kobj.parent = &parent_class->subsys.kobj;
577 585
578 error = kobject_add(&class_dev->kobj); 586 error = kobject_add(&class_dev->kobj, class_dev->kobj.parent,
587 "%s", class_dev->class_id);
579 if (error) 588 if (error)
580 goto out2; 589 goto out2;
581 590
@@ -642,7 +651,7 @@ int class_device_add(struct class_device *class_dev)
642 out3: 651 out3:
643 kobject_del(&class_dev->kobj); 652 kobject_del(&class_dev->kobj);
644 out2: 653 out2:
645 if(parent_class_dev) 654 if (parent_class_dev)
646 class_device_put(parent_class_dev); 655 class_device_put(parent_class_dev);
647 class_put(parent_class); 656 class_put(parent_class);
648 out1: 657 out1:
@@ -659,9 +668,11 @@ int class_device_register(struct class_device *class_dev)
659/** 668/**
660 * class_device_create - creates a class device and registers it with sysfs 669 * class_device_create - creates a class device and registers it with sysfs
661 * @cls: pointer to the struct class that this device should be registered to. 670 * @cls: pointer to the struct class that this device should be registered to.
662 * @parent: pointer to the parent struct class_device of this new device, if any. 671 * @parent: pointer to the parent struct class_device of this new device, if
672 * any.
663 * @devt: the dev_t for the char device to be added. 673 * @devt: the dev_t for the char device to be added.
664 * @device: a pointer to a struct device that is assiociated with this class device. 674 * @device: a pointer to a struct device that is assiociated with this class
675 * device.
665 * @fmt: string for the class device's name 676 * @fmt: string for the class device's name
666 * 677 *
667 * This function can be used by char device classes. A struct 678 * This function can be used by char device classes. A struct
@@ -785,7 +796,7 @@ void class_device_destroy(struct class *cls, dev_t devt)
785 class_device_unregister(class_dev); 796 class_device_unregister(class_dev);
786} 797}
787 798
788struct class_device * class_device_get(struct class_device *class_dev) 799struct class_device *class_device_get(struct class_device *class_dev)
789{ 800{
790 if (class_dev) 801 if (class_dev)
791 return to_class_dev(kobject_get(&class_dev->kobj)); 802 return to_class_dev(kobject_get(&class_dev->kobj));
@@ -798,6 +809,139 @@ void class_device_put(struct class_device *class_dev)
798 kobject_put(&class_dev->kobj); 809 kobject_put(&class_dev->kobj);
799} 810}
800 811
812/**
813 * class_for_each_device - device iterator
814 * @class: the class we're iterating
815 * @data: data for the callback
816 * @fn: function to be called for each device
817 *
818 * Iterate over @class's list of devices, and call @fn for each,
819 * passing it @data.
820 *
821 * We check the return of @fn each time. If it returns anything
822 * other than 0, we break out and return that value.
823 *
824 * Note, we hold class->sem in this function, so it can not be
825 * re-acquired in @fn, otherwise it will self-deadlocking. For
826 * example, calls to add or remove class members would be verboten.
827 */
828int class_for_each_device(struct class *class, void *data,
829 int (*fn)(struct device *, void *))
830{
831 struct device *dev;
832 int error = 0;
833
834 if (!class)
835 return -EINVAL;
836 down(&class->sem);
837 list_for_each_entry(dev, &class->devices, node) {
838 dev = get_device(dev);
839 if (dev) {
840 error = fn(dev, data);
841 put_device(dev);
842 } else
843 error = -ENODEV;
844 if (error)
845 break;
846 }
847 up(&class->sem);
848
849 return error;
850}
851EXPORT_SYMBOL_GPL(class_for_each_device);
852
853/**
854 * class_find_device - device iterator for locating a particular device
855 * @class: the class we're iterating
856 * @data: data for the match function
857 * @match: function to check device
858 *
859 * This is similar to the class_for_each_dev() function above, but it
860 * returns a reference to a device that is 'found' for later use, as
861 * determined by the @match callback.
862 *
863 * The callback should return 0 if the device doesn't match and non-zero
864 * if it does. If the callback returns non-zero, this function will
865 * return to the caller and not iterate over any more devices.
866
867 * Note, you will need to drop the reference with put_device() after use.
868 *
869 * We hold class->sem in this function, so it can not be
870 * re-acquired in @match, otherwise it will self-deadlocking. For
871 * example, calls to add or remove class members would be verboten.
872 */
873struct device *class_find_device(struct class *class, void *data,
874 int (*match)(struct device *, void *))
875{
876 struct device *dev;
877 int found = 0;
878
879 if (!class)
880 return NULL;
881
882 down(&class->sem);
883 list_for_each_entry(dev, &class->devices, node) {
884 dev = get_device(dev);
885 if (dev) {
886 if (match(dev, data)) {
887 found = 1;
888 break;
889 } else
890 put_device(dev);
891 } else
892 break;
893 }
894 up(&class->sem);
895
896 return found ? dev : NULL;
897}
898EXPORT_SYMBOL_GPL(class_find_device);
899
900/**
901 * class_find_child - device iterator for locating a particular class_device
902 * @class: the class we're iterating
903 * @data: data for the match function
904 * @match: function to check class_device
905 *
906 * This function returns a reference to a class_device that is 'found' for
907 * later use, as determined by the @match callback.
908 *
909 * The callback should return 0 if the class_device doesn't match and non-zero
910 * if it does. If the callback returns non-zero, this function will
911 * return to the caller and not iterate over any more class_devices.
912 *
913 * Note, you will need to drop the reference with class_device_put() after use.
914 *
915 * We hold class->sem in this function, so it can not be
916 * re-acquired in @match, otherwise it will self-deadlocking. For
917 * example, calls to add or remove class members would be verboten.
918 */
919struct class_device *class_find_child(struct class *class, void *data,
920 int (*match)(struct class_device *, void *))
921{
922 struct class_device *dev;
923 int found = 0;
924
925 if (!class)
926 return NULL;
927
928 down(&class->sem);
929 list_for_each_entry(dev, &class->children, node) {
930 dev = class_device_get(dev);
931 if (dev) {
932 if (match(dev, data)) {
933 found = 1;
934 break;
935 } else
936 class_device_put(dev);
937 } else
938 break;
939 }
940 up(&class->sem);
941
942 return found ? dev : NULL;
943}
944EXPORT_SYMBOL_GPL(class_find_child);
801 945
802int class_interface_register(struct class_interface *class_intf) 946int class_interface_register(struct class_interface *class_intf)
803{ 947{
@@ -829,7 +973,7 @@ int class_interface_register(struct class_interface *class_intf)
829 973
830void class_interface_unregister(struct class_interface *class_intf) 974void class_interface_unregister(struct class_interface *class_intf)
831{ 975{
832 struct class * parent = class_intf->class; 976 struct class *parent = class_intf->class;
833 struct class_device *class_dev; 977 struct class_device *class_dev;
834 struct device *dev; 978 struct device *dev;
835 979
@@ -853,15 +997,14 @@ void class_interface_unregister(struct class_interface *class_intf)
853 997
854int __init classes_init(void) 998int __init classes_init(void)
855{ 999{
856 int retval; 1000 class_kset = kset_create_and_add("class", NULL, NULL);
857 1001 if (!class_kset)
858 retval = subsystem_register(&class_subsys); 1002 return -ENOMEM;
859 if (retval)
860 return retval;
861 1003
862 /* ick, this is ugly, the things we go through to keep from showing up 1004 /* ick, this is ugly, the things we go through to keep from showing up
863 * in sysfs... */ 1005 * in sysfs... */
864 kset_init(&class_obj_subsys); 1006 kset_init(&class_obj_subsys);
1007 kobject_set_name(&class_obj_subsys.kobj, "class_obj");
865 if (!class_obj_subsys.kobj.parent) 1008 if (!class_obj_subsys.kobj.parent)
866 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj; 1009 class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
867 return 0; 1010 return 0;
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 2683eac30c68..edf3bbeb8d6a 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -18,14 +18,14 @@
18#include <linux/string.h> 18#include <linux/string.h>
19#include <linux/kdev_t.h> 19#include <linux/kdev_t.h>
20#include <linux/notifier.h> 20#include <linux/notifier.h>
21 21#include <linux/genhd.h>
22#include <asm/semaphore.h> 22#include <asm/semaphore.h>
23 23
24#include "base.h" 24#include "base.h"
25#include "power/power.h" 25#include "power/power.h"
26 26
27int (*platform_notify)(struct device * dev) = NULL; 27int (*platform_notify)(struct device *dev) = NULL;
28int (*platform_notify_remove)(struct device * dev) = NULL; 28int (*platform_notify_remove)(struct device *dev) = NULL;
29 29
30/* 30/*
31 * sysfs bindings for devices. 31 * sysfs bindings for devices.
@@ -51,11 +51,11 @@ EXPORT_SYMBOL(dev_driver_string);
51#define to_dev(obj) container_of(obj, struct device, kobj) 51#define to_dev(obj) container_of(obj, struct device, kobj)
52#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 52#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
53 53
54static ssize_t 54static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
55dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) 55 char *buf)
56{ 56{
57 struct device_attribute * dev_attr = to_dev_attr(attr); 57 struct device_attribute *dev_attr = to_dev_attr(attr);
58 struct device * dev = to_dev(kobj); 58 struct device *dev = to_dev(kobj);
59 ssize_t ret = -EIO; 59 ssize_t ret = -EIO;
60 60
61 if (dev_attr->show) 61 if (dev_attr->show)
@@ -63,12 +63,11 @@ dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
63 return ret; 63 return ret;
64} 64}
65 65
66static ssize_t 66static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
67dev_attr_store(struct kobject * kobj, struct attribute * attr, 67 const char *buf, size_t count)
68 const char * buf, size_t count)
69{ 68{
70 struct device_attribute * dev_attr = to_dev_attr(attr); 69 struct device_attribute *dev_attr = to_dev_attr(attr);
71 struct device * dev = to_dev(kobj); 70 struct device *dev = to_dev(kobj);
72 ssize_t ret = -EIO; 71 ssize_t ret = -EIO;
73 72
74 if (dev_attr->store) 73 if (dev_attr->store)
@@ -90,9 +89,9 @@ static struct sysfs_ops dev_sysfs_ops = {
90 * reaches 0. We forward the call to the device's release 89 * reaches 0. We forward the call to the device's release
91 * method, which should handle actually freeing the structure. 90 * method, which should handle actually freeing the structure.
92 */ 91 */
93static void device_release(struct kobject * kobj) 92static void device_release(struct kobject *kobj)
94{ 93{
95 struct device * dev = to_dev(kobj); 94 struct device *dev = to_dev(kobj);
96 95
97 if (dev->release) 96 if (dev->release)
98 dev->release(dev); 97 dev->release(dev);
@@ -101,8 +100,8 @@ static void device_release(struct kobject * kobj)
101 else if (dev->class && dev->class->dev_release) 100 else if (dev->class && dev->class->dev_release)
102 dev->class->dev_release(dev); 101 dev->class->dev_release(dev);
103 else { 102 else {
104 printk(KERN_ERR "Device '%s' does not have a release() function, " 103 printk(KERN_ERR "Device '%s' does not have a release() "
105 "it is broken and must be fixed.\n", 104 "function, it is broken and must be fixed.\n",
106 dev->bus_id); 105 dev->bus_id);
107 WARN_ON(1); 106 WARN_ON(1);
108 } 107 }
@@ -185,7 +184,8 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
185 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); 184 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
186 185
187 if (dev->driver) 186 if (dev->driver)
188 add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name); 187 add_uevent_var(env, "PHYSDEVDRIVER=%s",
188 dev->driver->name);
189 } 189 }
190#endif 190#endif
191 191
@@ -193,15 +193,16 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
193 if (dev->bus && dev->bus->uevent) { 193 if (dev->bus && dev->bus->uevent) {
194 retval = dev->bus->uevent(dev, env); 194 retval = dev->bus->uevent(dev, env);
195 if (retval) 195 if (retval)
196 pr_debug ("%s: bus uevent() returned %d\n", 196 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
197 __FUNCTION__, retval); 197 dev->bus_id, __FUNCTION__, retval);
198 } 198 }
199 199
200 /* have the class specific function add its stuff */ 200 /* have the class specific function add its stuff */
201 if (dev->class && dev->class->dev_uevent) { 201 if (dev->class && dev->class->dev_uevent) {
202 retval = dev->class->dev_uevent(dev, env); 202 retval = dev->class->dev_uevent(dev, env);
203 if (retval) 203 if (retval)
204 pr_debug("%s: class uevent() returned %d\n", 204 pr_debug("device: '%s': %s: class uevent() "
205 "returned %d\n", dev->bus_id,
205 __FUNCTION__, retval); 206 __FUNCTION__, retval);
206 } 207 }
207 208
@@ -209,7 +210,8 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
209 if (dev->type && dev->type->uevent) { 210 if (dev->type && dev->type->uevent) {
210 retval = dev->type->uevent(dev, env); 211 retval = dev->type->uevent(dev, env);
211 if (retval) 212 if (retval)
212 pr_debug("%s: dev_type uevent() returned %d\n", 213 pr_debug("device: '%s': %s: dev_type uevent() "
214 "returned %d\n", dev->bus_id,
213 __FUNCTION__, retval); 215 __FUNCTION__, retval);
214 } 216 }
215 217
@@ -325,7 +327,8 @@ static int device_add_groups(struct device *dev,
325 error = sysfs_create_group(&dev->kobj, groups[i]); 327 error = sysfs_create_group(&dev->kobj, groups[i]);
326 if (error) { 328 if (error) {
327 while (--i >= 0) 329 while (--i >= 0)
328 sysfs_remove_group(&dev->kobj, groups[i]); 330 sysfs_remove_group(&dev->kobj,
331 groups[i]);
329 break; 332 break;
330 } 333 }
331 } 334 }
@@ -401,20 +404,15 @@ static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
401static struct device_attribute devt_attr = 404static struct device_attribute devt_attr =
402 __ATTR(dev, S_IRUGO, show_dev, NULL); 405 __ATTR(dev, S_IRUGO, show_dev, NULL);
403 406
404/* 407/* kset to create /sys/devices/ */
405 * devices_subsys - structure to be registered with kobject core. 408struct kset *devices_kset;
406 */
407
408decl_subsys(devices, &device_ktype, &device_uevent_ops);
409
410 409
411/** 410/**
412 * device_create_file - create sysfs attribute file for device. 411 * device_create_file - create sysfs attribute file for device.
413 * @dev: device. 412 * @dev: device.
414 * @attr: device attribute descriptor. 413 * @attr: device attribute descriptor.
415 */ 414 */
416 415int device_create_file(struct device *dev, struct device_attribute *attr)
417int device_create_file(struct device * dev, struct device_attribute * attr)
418{ 416{
419 int error = 0; 417 int error = 0;
420 if (get_device(dev)) { 418 if (get_device(dev)) {
@@ -425,12 +423,11 @@ int device_create_file(struct device * dev, struct device_attribute * attr)
425} 423}
426 424
427/** 425/**
428 * device_remove_file - remove sysfs attribute file. 426 * device_remove_file - remove sysfs attribute file.
429 * @dev: device. 427 * @dev: device.
430 * @attr: device attribute descriptor. 428 * @attr: device attribute descriptor.
431 */ 429 */
432 430void device_remove_file(struct device *dev, struct device_attribute *attr)
433void device_remove_file(struct device * dev, struct device_attribute * attr)
434{ 431{
435 if (get_device(dev)) { 432 if (get_device(dev)) {
436 sysfs_remove_file(&dev->kobj, &attr->attr); 433 sysfs_remove_file(&dev->kobj, &attr->attr);
@@ -511,22 +508,20 @@ static void klist_children_put(struct klist_node *n)
511 put_device(dev); 508 put_device(dev);
512} 509}
513 510
514
515/** 511/**
516 * device_initialize - init device structure. 512 * device_initialize - init device structure.
517 * @dev: device. 513 * @dev: device.
518 * 514 *
519 * This prepares the device for use by other layers, 515 * This prepares the device for use by other layers,
520 * including adding it to the device hierarchy. 516 * including adding it to the device hierarchy.
521 * It is the first half of device_register(), if called by 517 * It is the first half of device_register(), if called by
522 * that, though it can also be called separately, so one 518 * that, though it can also be called separately, so one
523 * may use @dev's fields (e.g. the refcount). 519 * may use @dev's fields (e.g. the refcount).
524 */ 520 */
525
526void device_initialize(struct device *dev) 521void device_initialize(struct device *dev)
527{ 522{
528 kobj_set_kset_s(dev, devices_subsys); 523 dev->kobj.kset = devices_kset;
529 kobject_init(&dev->kobj); 524 kobject_init(&dev->kobj, &device_ktype);
530 klist_init(&dev->klist_children, klist_children_get, 525 klist_init(&dev->klist_children, klist_children_get,
531 klist_children_put); 526 klist_children_put);
532 INIT_LIST_HEAD(&dev->dma_pools); 527 INIT_LIST_HEAD(&dev->dma_pools);
@@ -539,36 +534,39 @@ void device_initialize(struct device *dev)
539} 534}
540 535
541#ifdef CONFIG_SYSFS_DEPRECATED 536#ifdef CONFIG_SYSFS_DEPRECATED
542static struct kobject * get_device_parent(struct device *dev, 537static struct kobject *get_device_parent(struct device *dev,
543 struct device *parent) 538 struct device *parent)
544{ 539{
545 /* 540 /* class devices without a parent live in /sys/class/<classname>/ */
546 * Set the parent to the class, not the parent device
547 * for topmost devices in class hierarchy.
548 * This keeps sysfs from having a symlink to make old
549 * udevs happy
550 */
551 if (dev->class && (!parent || parent->class != dev->class)) 541 if (dev->class && (!parent || parent->class != dev->class))
552 return &dev->class->subsys.kobj; 542 return &dev->class->subsys.kobj;
543 /* all other devices keep their parent */
553 else if (parent) 544 else if (parent)
554 return &parent->kobj; 545 return &parent->kobj;
555 546
556 return NULL; 547 return NULL;
557} 548}
549
550static inline void cleanup_device_parent(struct device *dev) {}
551static inline void cleanup_glue_dir(struct device *dev,
552 struct kobject *glue_dir) {}
558#else 553#else
559static struct kobject *virtual_device_parent(struct device *dev) 554static struct kobject *virtual_device_parent(struct device *dev)
560{ 555{
561 static struct kobject *virtual_dir = NULL; 556 static struct kobject *virtual_dir = NULL;
562 557
563 if (!virtual_dir) 558 if (!virtual_dir)
564 virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual"); 559 virtual_dir = kobject_create_and_add("virtual",
560 &devices_kset->kobj);
565 561
566 return virtual_dir; 562 return virtual_dir;
567} 563}
568 564
569static struct kobject * get_device_parent(struct device *dev, 565static struct kobject *get_device_parent(struct device *dev,
570 struct device *parent) 566 struct device *parent)
571{ 567{
568 int retval;
569
572 if (dev->class) { 570 if (dev->class) {
573 struct kobject *kobj = NULL; 571 struct kobject *kobj = NULL;
574 struct kobject *parent_kobj; 572 struct kobject *parent_kobj;
@@ -576,8 +574,8 @@ static struct kobject * get_device_parent(struct device *dev,
576 574
577 /* 575 /*
578 * If we have no parent, we live in "virtual". 576 * If we have no parent, we live in "virtual".
579 * Class-devices with a bus-device as parent, live 577 * Class-devices with a non class-device as parent, live
580 * in a class-directory to prevent namespace collisions. 578 * in a "glue" directory to prevent namespace collisions.
581 */ 579 */
582 if (parent == NULL) 580 if (parent == NULL)
583 parent_kobj = virtual_device_parent(dev); 581 parent_kobj = virtual_device_parent(dev);
@@ -598,25 +596,45 @@ static struct kobject * get_device_parent(struct device *dev,
598 return kobj; 596 return kobj;
599 597
600 /* or create a new class-directory at the parent device */ 598 /* or create a new class-directory at the parent device */
601 return kobject_kset_add_dir(&dev->class->class_dirs, 599 k = kobject_create();
602 parent_kobj, dev->class->name); 600 if (!k)
601 return NULL;
602 k->kset = &dev->class->class_dirs;
603 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
604 if (retval < 0) {
605 kobject_put(k);
606 return NULL;
607 }
608 /* do not emit an uevent for this simple "glue" directory */
609 return k;
603 } 610 }
604 611
605 if (parent) 612 if (parent)
606 return &parent->kobj; 613 return &parent->kobj;
607 return NULL; 614 return NULL;
608} 615}
616
617static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
618{
619 /* see if we live in a "glue" directory */
620 if (!dev->class || glue_dir->kset != &dev->class->class_dirs)
621 return;
622
623 kobject_put(glue_dir);
624}
625
626static void cleanup_device_parent(struct device *dev)
627{
628 cleanup_glue_dir(dev, dev->kobj.parent);
629}
609#endif 630#endif
610 631
611static int setup_parent(struct device *dev, struct device *parent) 632static void setup_parent(struct device *dev, struct device *parent)
612{ 633{
613 struct kobject *kobj; 634 struct kobject *kobj;
614 kobj = get_device_parent(dev, parent); 635 kobj = get_device_parent(dev, parent);
615 if (IS_ERR(kobj))
616 return PTR_ERR(kobj);
617 if (kobj) 636 if (kobj)
618 dev->kobj.parent = kobj; 637 dev->kobj.parent = kobj;
619 return 0;
620} 638}
621 639
622static int device_add_class_symlinks(struct device *dev) 640static int device_add_class_symlinks(struct device *dev)
@@ -625,65 +643,76 @@ static int device_add_class_symlinks(struct device *dev)
625 643
626 if (!dev->class) 644 if (!dev->class)
627 return 0; 645 return 0;
646
628 error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj, 647 error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
629 "subsystem"); 648 "subsystem");
630 if (error) 649 if (error)
631 goto out; 650 goto out;
632 /* 651
633 * If this is not a "fake" compatible device, then create the 652#ifdef CONFIG_SYSFS_DEPRECATED
634 * symlink from the class to the device. 653 /* stacked class devices need a symlink in the class directory */
635 */ 654 if (dev->kobj.parent != &dev->class->subsys.kobj &&
636 if (dev->kobj.parent != &dev->class->subsys.kobj) { 655 dev->type != &part_type) {
637 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, 656 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
638 dev->bus_id); 657 dev->bus_id);
639 if (error) 658 if (error)
640 goto out_subsys; 659 goto out_subsys;
641 } 660 }
642 if (dev->parent) {
643#ifdef CONFIG_SYSFS_DEPRECATED
644 {
645 struct device *parent = dev->parent;
646 char *class_name;
647
648 /*
649 * In old sysfs stacked class devices had 'device'
650 * link pointing to real device instead of parent
651 */
652 while (parent->class && !parent->bus && parent->parent)
653 parent = parent->parent;
654
655 error = sysfs_create_link(&dev->kobj,
656 &parent->kobj,
657 "device");
658 if (error)
659 goto out_busid;
660 661
661 class_name = make_class_name(dev->class->name, 662 if (dev->parent && dev->type != &part_type) {
662 &dev->kobj); 663 struct device *parent = dev->parent;
663 if (class_name) 664 char *class_name;
664 error = sysfs_create_link(&dev->parent->kobj, 665
665 &dev->kobj, class_name); 666 /*
666 kfree(class_name); 667 * stacked class devices have the 'device' link
667 if (error) 668 * pointing to the bus device instead of the parent
668 goto out_device; 669 */
669 } 670 while (parent->class && !parent->bus && parent->parent)
670#else 671 parent = parent->parent;
671 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 672
673 error = sysfs_create_link(&dev->kobj,
674 &parent->kobj,
672 "device"); 675 "device");
673 if (error) 676 if (error)
674 goto out_busid; 677 goto out_busid;
675#endif 678
679 class_name = make_class_name(dev->class->name,
680 &dev->kobj);
681 if (class_name)
682 error = sysfs_create_link(&dev->parent->kobj,
683 &dev->kobj, class_name);
684 kfree(class_name);
685 if (error)
686 goto out_device;
676 } 687 }
677 return 0; 688 return 0;
678 689
679#ifdef CONFIG_SYSFS_DEPRECATED
680out_device: 690out_device:
681 if (dev->parent) 691 if (dev->parent && dev->type != &part_type)
682 sysfs_remove_link(&dev->kobj, "device"); 692 sysfs_remove_link(&dev->kobj, "device");
683#endif
684out_busid: 693out_busid:
685 if (dev->kobj.parent != &dev->class->subsys.kobj) 694 if (dev->kobj.parent != &dev->class->subsys.kobj &&
695 dev->type != &part_type)
686 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); 696 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
697#else
698 /* link in the class directory pointing to the device */
699 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
700 dev->bus_id);
701 if (error)
702 goto out_subsys;
703
704 if (dev->parent && dev->type != &part_type) {
705 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
706 "device");
707 if (error)
708 goto out_busid;
709 }
710 return 0;
711
712out_busid:
713 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
714#endif
715
687out_subsys: 716out_subsys:
688 sysfs_remove_link(&dev->kobj, "subsystem"); 717 sysfs_remove_link(&dev->kobj, "subsystem");
689out: 718out:
@@ -694,8 +723,9 @@ static void device_remove_class_symlinks(struct device *dev)
694{ 723{
695 if (!dev->class) 724 if (!dev->class)
696 return; 725 return;
697 if (dev->parent) { 726
698#ifdef CONFIG_SYSFS_DEPRECATED 727#ifdef CONFIG_SYSFS_DEPRECATED
728 if (dev->parent && dev->type != &part_type) {
699 char *class_name; 729 char *class_name;
700 730
701 class_name = make_class_name(dev->class->name, &dev->kobj); 731 class_name = make_class_name(dev->class->name, &dev->kobj);
@@ -703,45 +733,59 @@ static void device_remove_class_symlinks(struct device *dev)
703 sysfs_remove_link(&dev->parent->kobj, class_name); 733 sysfs_remove_link(&dev->parent->kobj, class_name);
704 kfree(class_name); 734 kfree(class_name);
705 } 735 }
706#endif
707 sysfs_remove_link(&dev->kobj, "device"); 736 sysfs_remove_link(&dev->kobj, "device");
708 } 737 }
709 if (dev->kobj.parent != &dev->class->subsys.kobj) 738
739 if (dev->kobj.parent != &dev->class->subsys.kobj &&
740 dev->type != &part_type)
710 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); 741 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
742#else
743 if (dev->parent && dev->type != &part_type)
744 sysfs_remove_link(&dev->kobj, "device");
745
746 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
747#endif
748
711 sysfs_remove_link(&dev->kobj, "subsystem"); 749 sysfs_remove_link(&dev->kobj, "subsystem");
712} 750}
713 751
714/** 752/**
715 * device_add - add device to device hierarchy. 753 * device_add - add device to device hierarchy.
716 * @dev: device. 754 * @dev: device.
717 * 755 *
718 * This is part 2 of device_register(), though may be called 756 * This is part 2 of device_register(), though may be called
719 * separately _iff_ device_initialize() has been called separately. 757 * separately _iff_ device_initialize() has been called separately.
720 * 758 *
721 * This adds it to the kobject hierarchy via kobject_add(), adds it 759 * This adds it to the kobject hierarchy via kobject_add(), adds it
722 * to the global and sibling lists for the device, then 760 * to the global and sibling lists for the device, then
723 * adds it to the other relevant subsystems of the driver model. 761 * adds it to the other relevant subsystems of the driver model.
724 */ 762 */
725int device_add(struct device *dev) 763int device_add(struct device *dev)
726{ 764{
727 struct device *parent = NULL; 765 struct device *parent = NULL;
728 struct class_interface *class_intf; 766 struct class_interface *class_intf;
729 int error = -EINVAL; 767 int error;
768
769 error = pm_sleep_lock();
770 if (error) {
771 dev_warn(dev, "Suspicious %s during suspend\n", __FUNCTION__);
772 dump_stack();
773 return error;
774 }
730 775
731 dev = get_device(dev); 776 dev = get_device(dev);
732 if (!dev || !strlen(dev->bus_id)) 777 if (!dev || !strlen(dev->bus_id)) {
778 error = -EINVAL;
733 goto Error; 779 goto Error;
780 }
734 781
735 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); 782 pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
736 783
737 parent = get_device(dev->parent); 784 parent = get_device(dev->parent);
738 error = setup_parent(dev, parent); 785 setup_parent(dev, parent);
739 if (error)
740 goto Error;
741 786
742 /* first, register with generic layer. */ 787 /* first, register with generic layer. */
743 kobject_set_name(&dev->kobj, "%s", dev->bus_id); 788 error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
744 error = kobject_add(&dev->kobj);
745 if (error) 789 if (error)
746 goto Error; 790 goto Error;
747 791
@@ -751,7 +795,7 @@ int device_add(struct device *dev)
751 795
752 /* notify clients of device entry (new way) */ 796 /* notify clients of device entry (new way) */
753 if (dev->bus) 797 if (dev->bus)
754 blocking_notifier_call_chain(&dev->bus->bus_notifier, 798 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
755 BUS_NOTIFY_ADD_DEVICE, dev); 799 BUS_NOTIFY_ADD_DEVICE, dev);
756 800
757 error = device_create_file(dev, &uevent_attr); 801 error = device_create_file(dev, &uevent_attr);
@@ -795,13 +839,14 @@ int device_add(struct device *dev)
795 } 839 }
796 Done: 840 Done:
797 put_device(dev); 841 put_device(dev);
842 pm_sleep_unlock();
798 return error; 843 return error;
799 BusError: 844 BusError:
800 device_pm_remove(dev); 845 device_pm_remove(dev);
801 dpm_sysfs_remove(dev); 846 dpm_sysfs_remove(dev);
802 PMError: 847 PMError:
803 if (dev->bus) 848 if (dev->bus)
804 blocking_notifier_call_chain(&dev->bus->bus_notifier, 849 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
805 BUS_NOTIFY_DEL_DEVICE, dev); 850 BUS_NOTIFY_DEL_DEVICE, dev);
806 device_remove_attrs(dev); 851 device_remove_attrs(dev);
807 AttrsError: 852 AttrsError:
@@ -809,124 +854,84 @@ int device_add(struct device *dev)
809 SymlinkError: 854 SymlinkError:
810 if (MAJOR(dev->devt)) 855 if (MAJOR(dev->devt))
811 device_remove_file(dev, &devt_attr); 856 device_remove_file(dev, &devt_attr);
812
813 if (dev->class) {
814 sysfs_remove_link(&dev->kobj, "subsystem");
815 /* If this is not a "fake" compatible device, remove the
816 * symlink from the class to the device. */
817 if (dev->kobj.parent != &dev->class->subsys.kobj)
818 sysfs_remove_link(&dev->class->subsys.kobj,
819 dev->bus_id);
820 if (parent) {
821#ifdef CONFIG_SYSFS_DEPRECATED
822 char *class_name = make_class_name(dev->class->name,
823 &dev->kobj);
824 if (class_name)
825 sysfs_remove_link(&dev->parent->kobj,
826 class_name);
827 kfree(class_name);
828#endif
829 sysfs_remove_link(&dev->kobj, "device");
830 }
831 }
832 ueventattrError: 857 ueventattrError:
833 device_remove_file(dev, &uevent_attr); 858 device_remove_file(dev, &uevent_attr);
834 attrError: 859 attrError:
835 kobject_uevent(&dev->kobj, KOBJ_REMOVE); 860 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
836 kobject_del(&dev->kobj); 861 kobject_del(&dev->kobj);
837 Error: 862 Error:
863 cleanup_device_parent(dev);
838 if (parent) 864 if (parent)
839 put_device(parent); 865 put_device(parent);
840 goto Done; 866 goto Done;
841} 867}
842 868
843
844/** 869/**
845 * device_register - register a device with the system. 870 * device_register - register a device with the system.
846 * @dev: pointer to the device structure 871 * @dev: pointer to the device structure
847 * 872 *
848 * This happens in two clean steps - initialize the device 873 * This happens in two clean steps - initialize the device
849 * and add it to the system. The two steps can be called 874 * and add it to the system. The two steps can be called
850 * separately, but this is the easiest and most common. 875 * separately, but this is the easiest and most common.
851 * I.e. you should only call the two helpers separately if 876 * I.e. you should only call the two helpers separately if
852 * have a clearly defined need to use and refcount the device 877 * have a clearly defined need to use and refcount the device
853 * before it is added to the hierarchy. 878 * before it is added to the hierarchy.
854 */ 879 */
855
856int device_register(struct device *dev) 880int device_register(struct device *dev)
857{ 881{
858 device_initialize(dev); 882 device_initialize(dev);
859 return device_add(dev); 883 return device_add(dev);
860} 884}
861 885
862
863/** 886/**
864 * get_device - increment reference count for device. 887 * get_device - increment reference count for device.
865 * @dev: device. 888 * @dev: device.
866 * 889 *
867 * This simply forwards the call to kobject_get(), though 890 * This simply forwards the call to kobject_get(), though
868 * we do take care to provide for the case that we get a NULL 891 * we do take care to provide for the case that we get a NULL
869 * pointer passed in. 892 * pointer passed in.
870 */ 893 */
871 894struct device *get_device(struct device *dev)
872struct device * get_device(struct device * dev)
873{ 895{
874 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 896 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
875} 897}
876 898
877
878/** 899/**
879 * put_device - decrement reference count. 900 * put_device - decrement reference count.
880 * @dev: device in question. 901 * @dev: device in question.
881 */ 902 */
882void put_device(struct device * dev) 903void put_device(struct device *dev)
883{ 904{
905 /* might_sleep(); */
884 if (dev) 906 if (dev)
885 kobject_put(&dev->kobj); 907 kobject_put(&dev->kobj);
886} 908}
887 909
888
889/** 910/**
890 * device_del - delete device from system. 911 * device_del - delete device from system.
891 * @dev: device. 912 * @dev: device.
892 * 913 *
893 * This is the first part of the device unregistration 914 * This is the first part of the device unregistration
894 * sequence. This removes the device from the lists we control 915 * sequence. This removes the device from the lists we control
895 * from here, has it removed from the other driver model 916 * from here, has it removed from the other driver model
896 * subsystems it was added to in device_add(), and removes it 917 * subsystems it was added to in device_add(), and removes it
897 * from the kobject hierarchy. 918 * from the kobject hierarchy.
898 * 919 *
899 * NOTE: this should be called manually _iff_ device_add() was 920 * NOTE: this should be called manually _iff_ device_add() was
900 * also called manually. 921 * also called manually.
901 */ 922 */
902 923void device_del(struct device *dev)
903void device_del(struct device * dev)
904{ 924{
905 struct device * parent = dev->parent; 925 struct device *parent = dev->parent;
906 struct class_interface *class_intf; 926 struct class_interface *class_intf;
907 927
928 device_pm_remove(dev);
908 if (parent) 929 if (parent)
909 klist_del(&dev->knode_parent); 930 klist_del(&dev->knode_parent);
910 if (MAJOR(dev->devt)) 931 if (MAJOR(dev->devt))
911 device_remove_file(dev, &devt_attr); 932 device_remove_file(dev, &devt_attr);
912 if (dev->class) { 933 if (dev->class) {
913 sysfs_remove_link(&dev->kobj, "subsystem"); 934 device_remove_class_symlinks(dev);
914 /* If this is not a "fake" compatible device, remove the
915 * symlink from the class to the device. */
916 if (dev->kobj.parent != &dev->class->subsys.kobj)
917 sysfs_remove_link(&dev->class->subsys.kobj,
918 dev->bus_id);
919 if (parent) {
920#ifdef CONFIG_SYSFS_DEPRECATED
921 char *class_name = make_class_name(dev->class->name,
922 &dev->kobj);
923 if (class_name)
924 sysfs_remove_link(&dev->parent->kobj,
925 class_name);
926 kfree(class_name);
927#endif
928 sysfs_remove_link(&dev->kobj, "device");
929 }
930 935
931 down(&dev->class->sem); 936 down(&dev->class->sem);
932 /* notify any interfaces that the device is now gone */ 937 /* notify any interfaces that the device is now gone */
@@ -936,31 +941,6 @@ void device_del(struct device * dev)
936 /* remove the device from the class list */ 941 /* remove the device from the class list */
937 list_del_init(&dev->node); 942 list_del_init(&dev->node);
938 up(&dev->class->sem); 943 up(&dev->class->sem);
939
940 /* If we live in a parent class-directory, unreference it */
941 if (dev->kobj.parent->kset == &dev->class->class_dirs) {
942 struct device *d;
943 int other = 0;
944
945 /*
946 * if we are the last child of our class, delete
947 * our class-directory at this parent
948 */
949 down(&dev->class->sem);
950 list_for_each_entry(d, &dev->class->devices, node) {
951 if (d == dev)
952 continue;
953 if (d->kobj.parent == dev->kobj.parent) {
954 other = 1;
955 break;
956 }
957 }
958 if (!other)
959 kobject_del(dev->kobj.parent);
960
961 kobject_put(dev->kobj.parent);
962 up(&dev->class->sem);
963 }
964 } 944 }
965 device_remove_file(dev, &uevent_attr); 945 device_remove_file(dev, &uevent_attr);
966 device_remove_attrs(dev); 946 device_remove_attrs(dev);
@@ -979,57 +959,55 @@ void device_del(struct device * dev)
979 if (platform_notify_remove) 959 if (platform_notify_remove)
980 platform_notify_remove(dev); 960 platform_notify_remove(dev);
981 if (dev->bus) 961 if (dev->bus)
982 blocking_notifier_call_chain(&dev->bus->bus_notifier, 962 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
983 BUS_NOTIFY_DEL_DEVICE, dev); 963 BUS_NOTIFY_DEL_DEVICE, dev);
984 device_pm_remove(dev);
985 kobject_uevent(&dev->kobj, KOBJ_REMOVE); 964 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
965 cleanup_device_parent(dev);
986 kobject_del(&dev->kobj); 966 kobject_del(&dev->kobj);
987 if (parent) 967 put_device(parent);
988 put_device(parent);
989} 968}
990 969
991/** 970/**
992 * device_unregister - unregister device from system. 971 * device_unregister - unregister device from system.
993 * @dev: device going away. 972 * @dev: device going away.
994 * 973 *
995 * We do this in two parts, like we do device_register(). First, 974 * We do this in two parts, like we do device_register(). First,
996 * we remove it from all the subsystems with device_del(), then 975 * we remove it from all the subsystems with device_del(), then
997 * we decrement the reference count via put_device(). If that 976 * we decrement the reference count via put_device(). If that
998 * is the final reference count, the device will be cleaned up 977 * is the final reference count, the device will be cleaned up
999 * via device_release() above. Otherwise, the structure will 978 * via device_release() above. Otherwise, the structure will
1000 * stick around until the final reference to the device is dropped. 979 * stick around until the final reference to the device is dropped.
1001 */ 980 */
1002void device_unregister(struct device * dev) 981void device_unregister(struct device *dev)
1003{ 982{
1004 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); 983 pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
1005 device_del(dev); 984 device_del(dev);
1006 put_device(dev); 985 put_device(dev);
1007} 986}
1008 987
1009 988static struct device *next_device(struct klist_iter *i)
1010static struct device * next_device(struct klist_iter * i)
1011{ 989{
1012 struct klist_node * n = klist_next(i); 990 struct klist_node *n = klist_next(i);
1013 return n ? container_of(n, struct device, knode_parent) : NULL; 991 return n ? container_of(n, struct device, knode_parent) : NULL;
1014} 992}
1015 993
1016/** 994/**
1017 * device_for_each_child - device child iterator. 995 * device_for_each_child - device child iterator.
1018 * @parent: parent struct device. 996 * @parent: parent struct device.
1019 * @data: data for the callback. 997 * @data: data for the callback.
1020 * @fn: function to be called for each device. 998 * @fn: function to be called for each device.
1021 * 999 *
1022 * Iterate over @parent's child devices, and call @fn for each, 1000 * Iterate over @parent's child devices, and call @fn for each,
1023 * passing it @data. 1001 * passing it @data.
1024 * 1002 *
1025 * We check the return of @fn each time. If it returns anything 1003 * We check the return of @fn each time. If it returns anything
1026 * other than 0, we break out and return that value. 1004 * other than 0, we break out and return that value.
1027 */ 1005 */
1028int device_for_each_child(struct device * parent, void * data, 1006int device_for_each_child(struct device *parent, void *data,
1029 int (*fn)(struct device *, void *)) 1007 int (*fn)(struct device *dev, void *data))
1030{ 1008{
1031 struct klist_iter i; 1009 struct klist_iter i;
1032 struct device * child; 1010 struct device *child;
1033 int error = 0; 1011 int error = 0;
1034 1012
1035 klist_iter_init(&parent->klist_children, &i); 1013 klist_iter_init(&parent->klist_children, &i);
@@ -1054,8 +1032,8 @@ int device_for_each_child(struct device * parent, void * data,
1054 * current device can be obtained, this function will return to the caller 1032 * current device can be obtained, this function will return to the caller
1055 * and not iterate over any more devices. 1033 * and not iterate over any more devices.
1056 */ 1034 */
1057struct device * device_find_child(struct device *parent, void *data, 1035struct device *device_find_child(struct device *parent, void *data,
1058 int (*match)(struct device *, void *)) 1036 int (*match)(struct device *dev, void *data))
1059{ 1037{
1060 struct klist_iter i; 1038 struct klist_iter i;
1061 struct device *child; 1039 struct device *child;
@@ -1073,7 +1051,10 @@ struct device * device_find_child(struct device *parent, void *data,
1073 1051
1074int __init devices_init(void) 1052int __init devices_init(void)
1075{ 1053{
1076 return subsystem_register(&devices_subsys); 1054 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1055 if (!devices_kset)
1056 return -ENOMEM;
1057 return 0;
1077} 1058}
1078 1059
1079EXPORT_SYMBOL_GPL(device_for_each_child); 1060EXPORT_SYMBOL_GPL(device_for_each_child);
@@ -1094,7 +1075,7 @@ EXPORT_SYMBOL_GPL(device_remove_file);
1094 1075
1095static void device_create_release(struct device *dev) 1076static void device_create_release(struct device *dev)
1096{ 1077{
1097 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); 1078 pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
1098 kfree(dev); 1079 kfree(dev);
1099} 1080}
1100 1081
@@ -1156,14 +1137,11 @@ error:
1156EXPORT_SYMBOL_GPL(device_create); 1137EXPORT_SYMBOL_GPL(device_create);
1157 1138
1158/** 1139/**
1159 * device_destroy - removes a device that was created with device_create() 1140 * find_device - finds a device that was created with device_create()
1160 * @class: pointer to the struct class that this device was registered with 1141 * @class: pointer to the struct class that this device was registered with
1161 * @devt: the dev_t of the device that was previously registered 1142 * @devt: the dev_t of the device that was previously registered
1162 *
1163 * This call unregisters and cleans up a device that was created with a
1164 * call to device_create().
1165 */ 1143 */
1166void device_destroy(struct class *class, dev_t devt) 1144static struct device *find_device(struct class *class, dev_t devt)
1167{ 1145{
1168 struct device *dev = NULL; 1146 struct device *dev = NULL;
1169 struct device *dev_tmp; 1147 struct device *dev_tmp;
@@ -1176,12 +1154,54 @@ void device_destroy(struct class *class, dev_t devt)
1176 } 1154 }
1177 } 1155 }
1178 up(&class->sem); 1156 up(&class->sem);
1157 return dev;
1158}
1159
1160/**
1161 * device_destroy - removes a device that was created with device_create()
1162 * @class: pointer to the struct class that this device was registered with
1163 * @devt: the dev_t of the device that was previously registered
1164 *
1165 * This call unregisters and cleans up a device that was created with a
1166 * call to device_create().
1167 */
1168void device_destroy(struct class *class, dev_t devt)
1169{
1170 struct device *dev;
1179 1171
1172 dev = find_device(class, devt);
1180 if (dev) 1173 if (dev)
1181 device_unregister(dev); 1174 device_unregister(dev);
1182} 1175}
1183EXPORT_SYMBOL_GPL(device_destroy); 1176EXPORT_SYMBOL_GPL(device_destroy);
1184 1177
1178#ifdef CONFIG_PM_SLEEP
1179/**
1180 * destroy_suspended_device - asks the PM core to remove a suspended device
1181 * @class: pointer to the struct class that this device was registered with
1182 * @devt: the dev_t of the device that was previously registered
1183 *
1184 * This call notifies the PM core of the necessity to unregister a suspended
1185 * device created with a call to device_create() (devices cannot be
1186 * unregistered directly while suspended, since the PM core holds their
1187 * semaphores at that time).
1188 *
1189 * It can only be called within the scope of a system sleep transition. In
1190 * practice this means it has to be directly or indirectly invoked either by
1191 * a suspend or resume method, or by the PM core (e.g. via
1192 * disable_nonboot_cpus() or enable_nonboot_cpus()).
1193 */
1194void destroy_suspended_device(struct class *class, dev_t devt)
1195{
1196 struct device *dev;
1197
1198 dev = find_device(class, devt);
1199 if (dev)
1200 device_pm_schedule_removal(dev);
1201}
1202EXPORT_SYMBOL_GPL(destroy_suspended_device);
1203#endif /* CONFIG_PM_SLEEP */
1204
1185/** 1205/**
1186 * device_rename - renames a device 1206 * device_rename - renames a device
1187 * @dev: the pointer to the struct device to be renamed 1207 * @dev: the pointer to the struct device to be renamed
@@ -1198,7 +1218,8 @@ int device_rename(struct device *dev, char *new_name)
1198 if (!dev) 1218 if (!dev)
1199 return -EINVAL; 1219 return -EINVAL;
1200 1220
1201 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); 1221 pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
1222 __FUNCTION__, new_name);
1202 1223
1203#ifdef CONFIG_SYSFS_DEPRECATED 1224#ifdef CONFIG_SYSFS_DEPRECATED
1204 if ((dev->class) && (dev->parent)) 1225 if ((dev->class) && (dev->parent))
@@ -1279,8 +1300,7 @@ static int device_move_class_links(struct device *dev,
1279 class_name); 1300 class_name);
1280 if (error) 1301 if (error)
1281 sysfs_remove_link(&dev->kobj, "device"); 1302 sysfs_remove_link(&dev->kobj, "device");
1282 } 1303 } else
1283 else
1284 error = 0; 1304 error = 0;
1285out: 1305out:
1286 kfree(class_name); 1306 kfree(class_name);
@@ -1311,16 +1331,13 @@ int device_move(struct device *dev, struct device *new_parent)
1311 return -EINVAL; 1331 return -EINVAL;
1312 1332
1313 new_parent = get_device(new_parent); 1333 new_parent = get_device(new_parent);
1314 new_parent_kobj = get_device_parent (dev, new_parent); 1334 new_parent_kobj = get_device_parent(dev, new_parent);
1315 if (IS_ERR(new_parent_kobj)) { 1335
1316 error = PTR_ERR(new_parent_kobj); 1336 pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
1317 put_device(new_parent); 1337 __FUNCTION__, new_parent ? new_parent->bus_id : "<NULL>");
1318 goto out;
1319 }
1320 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1321 new_parent ? new_parent->bus_id : "<NULL>");
1322 error = kobject_move(&dev->kobj, new_parent_kobj); 1338 error = kobject_move(&dev->kobj, new_parent_kobj);
1323 if (error) { 1339 if (error) {
1340 cleanup_glue_dir(dev, new_parent_kobj);
1324 put_device(new_parent); 1341 put_device(new_parent);
1325 goto out; 1342 goto out;
1326 } 1343 }
@@ -1343,6 +1360,7 @@ int device_move(struct device *dev, struct device *new_parent)
1343 klist_add_tail(&dev->knode_parent, 1360 klist_add_tail(&dev->knode_parent,
1344 &old_parent->klist_children); 1361 &old_parent->klist_children);
1345 } 1362 }
1363 cleanup_glue_dir(dev, new_parent_kobj);
1346 put_device(new_parent); 1364 put_device(new_parent);
1347 goto out; 1365 goto out;
1348 } 1366 }
@@ -1352,5 +1370,23 @@ out:
1352 put_device(dev); 1370 put_device(dev);
1353 return error; 1371 return error;
1354} 1372}
1355
1356EXPORT_SYMBOL_GPL(device_move); 1373EXPORT_SYMBOL_GPL(device_move);
1374
1375/**
1376 * device_shutdown - call ->shutdown() on each device to shutdown.
1377 */
1378void device_shutdown(void)
1379{
1380 struct device *dev, *devn;
1381
1382 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1383 kobj.entry) {
1384 if (dev->bus && dev->bus->shutdown) {
1385 dev_dbg(dev, "shutdown\n");
1386 dev->bus->shutdown(dev);
1387 } else if (dev->driver && dev->driver->shutdown) {
1388 dev_dbg(dev, "shutdown\n");
1389 dev->driver->shutdown(dev);
1390 }
1391 }
1392}
diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c
index 40545071e3c9..c5885f5ce0ac 100644
--- a/drivers/base/cpu.c
+++ b/drivers/base/cpu.c
@@ -14,7 +14,7 @@
14#include "base.h" 14#include "base.h"
15 15
16struct sysdev_class cpu_sysdev_class = { 16struct sysdev_class cpu_sysdev_class = {
17 set_kset_name("cpu"), 17 .name = "cpu",
18}; 18};
19EXPORT_SYMBOL(cpu_sysdev_class); 19EXPORT_SYMBOL(cpu_sysdev_class);
20 20
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 7ac474db88c5..a5cde94bb982 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -1,18 +1,20 @@
1/* 1/*
2 * drivers/base/dd.c - The core device/driver interactions. 2 * drivers/base/dd.c - The core device/driver interactions.
3 * 3 *
4 * This file contains the (sometimes tricky) code that controls the 4 * This file contains the (sometimes tricky) code that controls the
5 * interactions between devices and drivers, which primarily includes 5 * interactions between devices and drivers, which primarily includes
6 * driver binding and unbinding. 6 * driver binding and unbinding.
7 * 7 *
8 * All of this code used to exist in drivers/base/bus.c, but was 8 * All of this code used to exist in drivers/base/bus.c, but was
9 * relocated to here in the name of compartmentalization (since it wasn't 9 * relocated to here in the name of compartmentalization (since it wasn't
10 * strictly code just for the 'struct bus_type'. 10 * strictly code just for the 'struct bus_type'.
11 * 11 *
12 * Copyright (c) 2002-5 Patrick Mochel 12 * Copyright (c) 2002-5 Patrick Mochel
13 * Copyright (c) 2002-3 Open Source Development Labs 13 * Copyright (c) 2002-3 Open Source Development Labs
14 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
15 * Copyright (c) 2007 Novell Inc.
14 * 16 *
15 * This file is released under the GPLv2 17 * This file is released under the GPLv2
16 */ 18 */
17 19
18#include <linux/device.h> 20#include <linux/device.h>
@@ -23,8 +25,6 @@
23#include "base.h" 25#include "base.h"
24#include "power/power.h" 26#include "power/power.h"
25 27
26#define to_drv(node) container_of(node, struct device_driver, kobj.entry)
27
28 28
29static void driver_bound(struct device *dev) 29static void driver_bound(struct device *dev)
30{ 30{
@@ -34,27 +34,27 @@ static void driver_bound(struct device *dev)
34 return; 34 return;
35 } 35 }
36 36
37 pr_debug("bound device '%s' to driver '%s'\n", 37 pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->bus_id,
38 dev->bus_id, dev->driver->name); 38 __FUNCTION__, dev->driver->name);
39 39
40 if (dev->bus) 40 if (dev->bus)
41 blocking_notifier_call_chain(&dev->bus->bus_notifier, 41 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
42 BUS_NOTIFY_BOUND_DRIVER, dev); 42 BUS_NOTIFY_BOUND_DRIVER, dev);
43 43
44 klist_add_tail(&dev->knode_driver, &dev->driver->klist_devices); 44 klist_add_tail(&dev->knode_driver, &dev->driver->p->klist_devices);
45} 45}
46 46
47static int driver_sysfs_add(struct device *dev) 47static int driver_sysfs_add(struct device *dev)
48{ 48{
49 int ret; 49 int ret;
50 50
51 ret = sysfs_create_link(&dev->driver->kobj, &dev->kobj, 51 ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
52 kobject_name(&dev->kobj)); 52 kobject_name(&dev->kobj));
53 if (ret == 0) { 53 if (ret == 0) {
54 ret = sysfs_create_link(&dev->kobj, &dev->driver->kobj, 54 ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
55 "driver"); 55 "driver");
56 if (ret) 56 if (ret)
57 sysfs_remove_link(&dev->driver->kobj, 57 sysfs_remove_link(&dev->driver->p->kobj,
58 kobject_name(&dev->kobj)); 58 kobject_name(&dev->kobj));
59 } 59 }
60 return ret; 60 return ret;
@@ -65,24 +65,24 @@ static void driver_sysfs_remove(struct device *dev)
65 struct device_driver *drv = dev->driver; 65 struct device_driver *drv = dev->driver;
66 66
67 if (drv) { 67 if (drv) {
68 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj)); 68 sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
69 sysfs_remove_link(&dev->kobj, "driver"); 69 sysfs_remove_link(&dev->kobj, "driver");
70 } 70 }
71} 71}
72 72
73/** 73/**
74 * device_bind_driver - bind a driver to one device. 74 * device_bind_driver - bind a driver to one device.
75 * @dev: device. 75 * @dev: device.
76 * 76 *
77 * Allow manual attachment of a driver to a device. 77 * Allow manual attachment of a driver to a device.
78 * Caller must have already set @dev->driver. 78 * Caller must have already set @dev->driver.
79 * 79 *
80 * Note that this does not modify the bus reference count 80 * Note that this does not modify the bus reference count
81 * nor take the bus's rwsem. Please verify those are accounted 81 * nor take the bus's rwsem. Please verify those are accounted
82 * for before calling this. (It is ok to call with no other effort 82 * for before calling this. (It is ok to call with no other effort
83 * from a driver's probe() method.) 83 * from a driver's probe() method.)
84 * 84 *
85 * This function must be called with @dev->sem held. 85 * This function must be called with @dev->sem held.
86 */ 86 */
87int device_bind_driver(struct device *dev) 87int device_bind_driver(struct device *dev)
88{ 88{
@@ -93,6 +93,7 @@ int device_bind_driver(struct device *dev)
93 driver_bound(dev); 93 driver_bound(dev);
94 return ret; 94 return ret;
95} 95}
96EXPORT_SYMBOL_GPL(device_bind_driver);
96 97
97static atomic_t probe_count = ATOMIC_INIT(0); 98static atomic_t probe_count = ATOMIC_INIT(0);
98static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue); 99static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
@@ -102,8 +103,8 @@ static int really_probe(struct device *dev, struct device_driver *drv)
102 int ret = 0; 103 int ret = 0;
103 104
104 atomic_inc(&probe_count); 105 atomic_inc(&probe_count);
105 pr_debug("%s: Probing driver %s with device %s\n", 106 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
106 drv->bus->name, drv->name, dev->bus_id); 107 drv->bus->name, __FUNCTION__, drv->name, dev->bus_id);
107 WARN_ON(!list_empty(&dev->devres_head)); 108 WARN_ON(!list_empty(&dev->devres_head));
108 109
109 dev->driver = drv; 110 dev->driver = drv;
@@ -125,8 +126,8 @@ static int really_probe(struct device *dev, struct device_driver *drv)
125 126
126 driver_bound(dev); 127 driver_bound(dev);
127 ret = 1; 128 ret = 1;
128 pr_debug("%s: Bound Device %s to Driver %s\n", 129 pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
129 drv->bus->name, dev->bus_id, drv->name); 130 drv->bus->name, __FUNCTION__, dev->bus_id, drv->name);
130 goto done; 131 goto done;
131 132
132probe_failed: 133probe_failed:
@@ -183,7 +184,7 @@ int driver_probe_done(void)
183 * This function must be called with @dev->sem held. When called for a 184 * This function must be called with @dev->sem held. When called for a
184 * USB interface, @dev->parent->sem must be held as well. 185 * USB interface, @dev->parent->sem must be held as well.
185 */ 186 */
186int driver_probe_device(struct device_driver * drv, struct device * dev) 187int driver_probe_device(struct device_driver *drv, struct device *dev)
187{ 188{
188 int ret = 0; 189 int ret = 0;
189 190
@@ -192,8 +193,8 @@ int driver_probe_device(struct device_driver * drv, struct device * dev)
192 if (drv->bus->match && !drv->bus->match(dev, drv)) 193 if (drv->bus->match && !drv->bus->match(dev, drv))
193 goto done; 194 goto done;
194 195
195 pr_debug("%s: Matched Device %s with Driver %s\n", 196 pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
196 drv->bus->name, dev->bus_id, drv->name); 197 drv->bus->name, __FUNCTION__, dev->bus_id, drv->name);
197 198
198 ret = really_probe(dev, drv); 199 ret = really_probe(dev, drv);
199 200
@@ -201,27 +202,27 @@ done:
201 return ret; 202 return ret;
202} 203}
203 204
204static int __device_attach(struct device_driver * drv, void * data) 205static int __device_attach(struct device_driver *drv, void *data)
205{ 206{
206 struct device * dev = data; 207 struct device *dev = data;
207 return driver_probe_device(drv, dev); 208 return driver_probe_device(drv, dev);
208} 209}
209 210
210/** 211/**
211 * device_attach - try to attach device to a driver. 212 * device_attach - try to attach device to a driver.
212 * @dev: device. 213 * @dev: device.
213 * 214 *
214 * Walk the list of drivers that the bus has and call 215 * Walk the list of drivers that the bus has and call
215 * driver_probe_device() for each pair. If a compatible 216 * driver_probe_device() for each pair. If a compatible
216 * pair is found, break out and return. 217 * pair is found, break out and return.
217 * 218 *
218 * Returns 1 if the device was bound to a driver; 219 * Returns 1 if the device was bound to a driver;
219 * 0 if no matching device was found; 220 * 0 if no matching device was found;
220 * -ENODEV if the device is not registered. 221 * -ENODEV if the device is not registered.
221 * 222 *
222 * When called for a USB interface, @dev->parent->sem must be held. 223 * When called for a USB interface, @dev->parent->sem must be held.
223 */ 224 */
224int device_attach(struct device * dev) 225int device_attach(struct device *dev)
225{ 226{
226 int ret = 0; 227 int ret = 0;
227 228
@@ -240,10 +241,11 @@ int device_attach(struct device * dev)
240 up(&dev->sem); 241 up(&dev->sem);
241 return ret; 242 return ret;
242} 243}
244EXPORT_SYMBOL_GPL(device_attach);
243 245
244static int __driver_attach(struct device * dev, void * data) 246static int __driver_attach(struct device *dev, void *data)
245{ 247{
246 struct device_driver * drv = data; 248 struct device_driver *drv = data;
247 249
248 /* 250 /*
249 * Lock device and try to bind to it. We drop the error 251 * Lock device and try to bind to it. We drop the error
@@ -268,35 +270,35 @@ static int __driver_attach(struct device * dev, void * data)
268} 270}
269 271
270/** 272/**
271 * driver_attach - try to bind driver to devices. 273 * driver_attach - try to bind driver to devices.
272 * @drv: driver. 274 * @drv: driver.
273 * 275 *
274 * Walk the list of devices that the bus has on it and try to 276 * Walk the list of devices that the bus has on it and try to
275 * match the driver with each one. If driver_probe_device() 277 * match the driver with each one. If driver_probe_device()
276 * returns 0 and the @dev->driver is set, we've found a 278 * returns 0 and the @dev->driver is set, we've found a
277 * compatible pair. 279 * compatible pair.
278 */ 280 */
279int driver_attach(struct device_driver * drv) 281int driver_attach(struct device_driver *drv)
280{ 282{
281 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); 283 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
282} 284}
285EXPORT_SYMBOL_GPL(driver_attach);
283 286
284/* 287/*
285 * __device_release_driver() must be called with @dev->sem held. 288 * __device_release_driver() must be called with @dev->sem held.
286 * When called for a USB interface, @dev->parent->sem must be held as well. 289 * When called for a USB interface, @dev->parent->sem must be held as well.
287 */ 290 */
288static void __device_release_driver(struct device * dev) 291static void __device_release_driver(struct device *dev)
289{ 292{
290 struct device_driver * drv; 293 struct device_driver *drv;
291 294
292 drv = get_driver(dev->driver); 295 drv = dev->driver;
293 if (drv) { 296 if (drv) {
294 driver_sysfs_remove(dev); 297 driver_sysfs_remove(dev);
295 sysfs_remove_link(&dev->kobj, "driver"); 298 sysfs_remove_link(&dev->kobj, "driver");
296 klist_remove(&dev->knode_driver);
297 299
298 if (dev->bus) 300 if (dev->bus)
299 blocking_notifier_call_chain(&dev->bus->bus_notifier, 301 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
300 BUS_NOTIFY_UNBIND_DRIVER, 302 BUS_NOTIFY_UNBIND_DRIVER,
301 dev); 303 dev);
302 304
@@ -306,18 +308,18 @@ static void __device_release_driver(struct device * dev)
306 drv->remove(dev); 308 drv->remove(dev);
307 devres_release_all(dev); 309 devres_release_all(dev);
308 dev->driver = NULL; 310 dev->driver = NULL;
309 put_driver(drv); 311 klist_remove(&dev->knode_driver);
310 } 312 }
311} 313}
312 314
313/** 315/**
314 * device_release_driver - manually detach device from driver. 316 * device_release_driver - manually detach device from driver.
315 * @dev: device. 317 * @dev: device.
316 * 318 *
317 * Manually detach device from driver. 319 * Manually detach device from driver.
318 * When called for a USB interface, @dev->parent->sem must be held. 320 * When called for a USB interface, @dev->parent->sem must be held.
319 */ 321 */
320void device_release_driver(struct device * dev) 322void device_release_driver(struct device *dev)
321{ 323{
322 /* 324 /*
323 * If anyone calls device_release_driver() recursively from 325 * If anyone calls device_release_driver() recursively from
@@ -328,26 +330,26 @@ void device_release_driver(struct device * dev)
328 __device_release_driver(dev); 330 __device_release_driver(dev);
329 up(&dev->sem); 331 up(&dev->sem);
330} 332}
331 333EXPORT_SYMBOL_GPL(device_release_driver);
332 334
333/** 335/**
334 * driver_detach - detach driver from all devices it controls. 336 * driver_detach - detach driver from all devices it controls.
335 * @drv: driver. 337 * @drv: driver.
336 */ 338 */
337void driver_detach(struct device_driver * drv) 339void driver_detach(struct device_driver *drv)
338{ 340{
339 struct device * dev; 341 struct device *dev;
340 342
341 for (;;) { 343 for (;;) {
342 spin_lock(&drv->klist_devices.k_lock); 344 spin_lock(&drv->p->klist_devices.k_lock);
343 if (list_empty(&drv->klist_devices.k_list)) { 345 if (list_empty(&drv->p->klist_devices.k_list)) {
344 spin_unlock(&drv->klist_devices.k_lock); 346 spin_unlock(&drv->p->klist_devices.k_lock);
345 break; 347 break;
346 } 348 }
347 dev = list_entry(drv->klist_devices.k_list.prev, 349 dev = list_entry(drv->p->klist_devices.k_list.prev,
348 struct device, knode_driver.n_node); 350 struct device, knode_driver.n_node);
349 get_device(dev); 351 get_device(dev);
350 spin_unlock(&drv->klist_devices.k_lock); 352 spin_unlock(&drv->p->klist_devices.k_lock);
351 353
352 if (dev->parent) /* Needed for USB */ 354 if (dev->parent) /* Needed for USB */
353 down(&dev->parent->sem); 355 down(&dev->parent->sem);
@@ -360,9 +362,3 @@ void driver_detach(struct device_driver * drv)
360 put_device(dev); 362 put_device(dev);
361 } 363 }
362} 364}
363
364EXPORT_SYMBOL_GPL(device_bind_driver);
365EXPORT_SYMBOL_GPL(device_release_driver);
366EXPORT_SYMBOL_GPL(device_attach);
367EXPORT_SYMBOL_GPL(driver_attach);
368
diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index eb11475293ed..a35f04121a00 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -3,6 +3,8 @@
3 * 3 *
4 * Copyright (c) 2002-3 Patrick Mochel 4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs 5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc.
6 * 8 *
7 * This file is released under the GPLv2 9 * This file is released under the GPLv2
8 * 10 *
@@ -15,46 +17,42 @@
15#include "base.h" 17#include "base.h"
16 18
17#define to_dev(node) container_of(node, struct device, driver_list) 19#define to_dev(node) container_of(node, struct device, driver_list)
18#define to_drv(obj) container_of(obj, struct device_driver, kobj)
19 20
20 21
21static struct device * next_device(struct klist_iter * i) 22static struct device *next_device(struct klist_iter *i)
22{ 23{
23 struct klist_node * n = klist_next(i); 24 struct klist_node *n = klist_next(i);
24 return n ? container_of(n, struct device, knode_driver) : NULL; 25 return n ? container_of(n, struct device, knode_driver) : NULL;
25} 26}
26 27
27/** 28/**
28 * driver_for_each_device - Iterator for devices bound to a driver. 29 * driver_for_each_device - Iterator for devices bound to a driver.
29 * @drv: Driver we're iterating. 30 * @drv: Driver we're iterating.
30 * @start: Device to begin with 31 * @start: Device to begin with
31 * @data: Data to pass to the callback. 32 * @data: Data to pass to the callback.
32 * @fn: Function to call for each device. 33 * @fn: Function to call for each device.
33 * 34 *
34 * Iterate over the @drv's list of devices calling @fn for each one. 35 * Iterate over the @drv's list of devices calling @fn for each one.
35 */ 36 */
36 37int driver_for_each_device(struct device_driver *drv, struct device *start,
37int driver_for_each_device(struct device_driver * drv, struct device * start, 38 void *data, int (*fn)(struct device *, void *))
38 void * data, int (*fn)(struct device *, void *))
39{ 39{
40 struct klist_iter i; 40 struct klist_iter i;
41 struct device * dev; 41 struct device *dev;
42 int error = 0; 42 int error = 0;
43 43
44 if (!drv) 44 if (!drv)
45 return -EINVAL; 45 return -EINVAL;
46 46
47 klist_iter_init_node(&drv->klist_devices, &i, 47 klist_iter_init_node(&drv->p->klist_devices, &i,
48 start ? &start->knode_driver : NULL); 48 start ? &start->knode_driver : NULL);
49 while ((dev = next_device(&i)) && !error) 49 while ((dev = next_device(&i)) && !error)
50 error = fn(dev, data); 50 error = fn(dev, data);
51 klist_iter_exit(&i); 51 klist_iter_exit(&i);
52 return error; 52 return error;
53} 53}
54
55EXPORT_SYMBOL_GPL(driver_for_each_device); 54EXPORT_SYMBOL_GPL(driver_for_each_device);
56 55
57
58/** 56/**
59 * driver_find_device - device iterator for locating a particular device. 57 * driver_find_device - device iterator for locating a particular device.
60 * @drv: The device's driver 58 * @drv: The device's driver
@@ -70,9 +68,9 @@ EXPORT_SYMBOL_GPL(driver_for_each_device);
70 * if it does. If the callback returns non-zero, this function will 68 * if it does. If the callback returns non-zero, this function will
71 * return to the caller and not iterate over any more devices. 69 * return to the caller and not iterate over any more devices.
72 */ 70 */
73struct device * driver_find_device(struct device_driver *drv, 71struct device *driver_find_device(struct device_driver *drv,
74 struct device * start, void * data, 72 struct device *start, void *data,
75 int (*match)(struct device *, void *)) 73 int (*match)(struct device *dev, void *data))
76{ 74{
77 struct klist_iter i; 75 struct klist_iter i;
78 struct device *dev; 76 struct device *dev;
@@ -80,7 +78,7 @@ struct device * driver_find_device(struct device_driver *drv,
80 if (!drv) 78 if (!drv)
81 return NULL; 79 return NULL;
82 80
83 klist_iter_init_node(&drv->klist_devices, &i, 81 klist_iter_init_node(&drv->p->klist_devices, &i,
84 (start ? &start->knode_driver : NULL)); 82 (start ? &start->knode_driver : NULL));
85 while ((dev = next_device(&i))) 83 while ((dev = next_device(&i)))
86 if (match(dev, data) && get_device(dev)) 84 if (match(dev, data) && get_device(dev))
@@ -91,111 +89,179 @@ struct device * driver_find_device(struct device_driver *drv,
91EXPORT_SYMBOL_GPL(driver_find_device); 89EXPORT_SYMBOL_GPL(driver_find_device);
92 90
93/** 91/**
94 * driver_create_file - create sysfs file for driver. 92 * driver_create_file - create sysfs file for driver.
95 * @drv: driver. 93 * @drv: driver.
96 * @attr: driver attribute descriptor. 94 * @attr: driver attribute descriptor.
97 */ 95 */
98 96int driver_create_file(struct device_driver *drv,
99int driver_create_file(struct device_driver * drv, struct driver_attribute * attr) 97 struct driver_attribute *attr)
100{ 98{
101 int error; 99 int error;
102 if (get_driver(drv)) { 100 if (get_driver(drv)) {
103 error = sysfs_create_file(&drv->kobj, &attr->attr); 101 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
104 put_driver(drv); 102 put_driver(drv);
105 } else 103 } else
106 error = -EINVAL; 104 error = -EINVAL;
107 return error; 105 return error;
108} 106}
109 107EXPORT_SYMBOL_GPL(driver_create_file);
110 108
111/** 109/**
112 * driver_remove_file - remove sysfs file for driver. 110 * driver_remove_file - remove sysfs file for driver.
113 * @drv: driver. 111 * @drv: driver.
114 * @attr: driver attribute descriptor. 112 * @attr: driver attribute descriptor.
115 */ 113 */
116 114void driver_remove_file(struct device_driver *drv,
117void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr) 115 struct driver_attribute *attr)
118{ 116{
119 if (get_driver(drv)) { 117 if (get_driver(drv)) {
120 sysfs_remove_file(&drv->kobj, &attr->attr); 118 sysfs_remove_file(&drv->p->kobj, &attr->attr);
121 put_driver(drv); 119 put_driver(drv);
122 } 120 }
123} 121}
124 122EXPORT_SYMBOL_GPL(driver_remove_file);
125 123
126/** 124/**
127 * get_driver - increment driver reference count. 125 * driver_add_kobj - add a kobject below the specified driver
128 * @drv: driver. 126 *
127 * You really don't want to do this, this is only here due to one looney
128 * iseries driver, go poke those developers if you are annoyed about
129 * this...
129 */ 130 */
130struct device_driver * get_driver(struct device_driver * drv) 131int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
132 const char *fmt, ...)
131{ 133{
132 return drv ? to_drv(kobject_get(&drv->kobj)) : NULL; 134 va_list args;
135 char *name;
136
137 va_start(args, fmt);
138 name = kvasprintf(GFP_KERNEL, fmt, args);
139 va_end(args);
140
141 if (!name)
142 return -ENOMEM;
143
144 return kobject_add(kobj, &drv->p->kobj, "%s", name);
133} 145}
146EXPORT_SYMBOL_GPL(driver_add_kobj);
147
148/**
149 * get_driver - increment driver reference count.
150 * @drv: driver.
151 */
152struct device_driver *get_driver(struct device_driver *drv)
153{
154 if (drv) {
155 struct driver_private *priv;
156 struct kobject *kobj;
134 157
158 kobj = kobject_get(&drv->p->kobj);
159 priv = to_driver(kobj);
160 return priv->driver;
161 }
162 return NULL;
163}
164EXPORT_SYMBOL_GPL(get_driver);
135 165
136/** 166/**
137 * put_driver - decrement driver's refcount. 167 * put_driver - decrement driver's refcount.
138 * @drv: driver. 168 * @drv: driver.
139 */ 169 */
140void put_driver(struct device_driver * drv) 170void put_driver(struct device_driver *drv)
171{
172 kobject_put(&drv->p->kobj);
173}
174EXPORT_SYMBOL_GPL(put_driver);
175
176static int driver_add_groups(struct device_driver *drv,
177 struct attribute_group **groups)
141{ 178{
142 kobject_put(&drv->kobj); 179 int error = 0;
180 int i;
181
182 if (groups) {
183 for (i = 0; groups[i]; i++) {
184 error = sysfs_create_group(&drv->p->kobj, groups[i]);
185 if (error) {
186 while (--i >= 0)
187 sysfs_remove_group(&drv->p->kobj,
188 groups[i]);
189 break;
190 }
191 }
192 }
193 return error;
194}
195
196static void driver_remove_groups(struct device_driver *drv,
197 struct attribute_group **groups)
198{
199 int i;
200
201 if (groups)
202 for (i = 0; groups[i]; i++)
203 sysfs_remove_group(&drv->p->kobj, groups[i]);
143} 204}
144 205
145/** 206/**
146 * driver_register - register driver with bus 207 * driver_register - register driver with bus
147 * @drv: driver to register 208 * @drv: driver to register
148 * 209 *
149 * We pass off most of the work to the bus_add_driver() call, 210 * We pass off most of the work to the bus_add_driver() call,
150 * since most of the things we have to do deal with the bus 211 * since most of the things we have to do deal with the bus
151 * structures. 212 * structures.
152 */ 213 */
153int driver_register(struct device_driver * drv) 214int driver_register(struct device_driver *drv)
154{ 215{
216 int ret;
217
155 if ((drv->bus->probe && drv->probe) || 218 if ((drv->bus->probe && drv->probe) ||
156 (drv->bus->remove && drv->remove) || 219 (drv->bus->remove && drv->remove) ||
157 (drv->bus->shutdown && drv->shutdown)) { 220 (drv->bus->shutdown && drv->shutdown))
158 printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name); 221 printk(KERN_WARNING "Driver '%s' needs updating - please use "
159 } 222 "bus_type methods\n", drv->name);
160 klist_init(&drv->klist_devices, NULL, NULL); 223 ret = bus_add_driver(drv);
161 return bus_add_driver(drv); 224 if (ret)
225 return ret;
226 ret = driver_add_groups(drv, drv->groups);
227 if (ret)
228 bus_remove_driver(drv);
229 return ret;
162} 230}
231EXPORT_SYMBOL_GPL(driver_register);
163 232
164/** 233/**
165 * driver_unregister - remove driver from system. 234 * driver_unregister - remove driver from system.
166 * @drv: driver. 235 * @drv: driver.
167 * 236 *
168 * Again, we pass off most of the work to the bus-level call. 237 * Again, we pass off most of the work to the bus-level call.
169 */ 238 */
170 239void driver_unregister(struct device_driver *drv)
171void driver_unregister(struct device_driver * drv)
172{ 240{
241 driver_remove_groups(drv, drv->groups);
173 bus_remove_driver(drv); 242 bus_remove_driver(drv);
174} 243}
244EXPORT_SYMBOL_GPL(driver_unregister);
175 245
176/** 246/**
177 * driver_find - locate driver on a bus by its name. 247 * driver_find - locate driver on a bus by its name.
178 * @name: name of the driver. 248 * @name: name of the driver.
179 * @bus: bus to scan for the driver. 249 * @bus: bus to scan for the driver.
180 * 250 *
181 * Call kset_find_obj() to iterate over list of drivers on 251 * Call kset_find_obj() to iterate over list of drivers on
182 * a bus to find driver by name. Return driver if found. 252 * a bus to find driver by name. Return driver if found.
183 * 253 *
184 * Note that kset_find_obj increments driver's reference count. 254 * Note that kset_find_obj increments driver's reference count.
185 */ 255 */
186struct device_driver *driver_find(const char *name, struct bus_type *bus) 256struct device_driver *driver_find(const char *name, struct bus_type *bus)
187{ 257{
188 struct kobject *k = kset_find_obj(&bus->drivers, name); 258 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
189 if (k) 259 struct driver_private *priv;
190 return to_drv(k); 260
261 if (k) {
262 priv = to_driver(k);
263 return priv->driver;
264 }
191 return NULL; 265 return NULL;
192} 266}
193
194EXPORT_SYMBOL_GPL(driver_register);
195EXPORT_SYMBOL_GPL(driver_unregister);
196EXPORT_SYMBOL_GPL(get_driver);
197EXPORT_SYMBOL_GPL(put_driver);
198EXPORT_SYMBOL_GPL(driver_find); 267EXPORT_SYMBOL_GPL(driver_find);
199
200EXPORT_SYMBOL_GPL(driver_create_file);
201EXPORT_SYMBOL_GPL(driver_remove_file);
diff --git a/drivers/base/firmware.c b/drivers/base/firmware.c
index 90c862932169..113815556809 100644
--- a/drivers/base/firmware.c
+++ b/drivers/base/firmware.c
@@ -3,11 +3,11 @@
3 * 3 *
4 * Copyright (c) 2002-3 Patrick Mochel 4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs 5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc.
6 * 8 *
7 * This file is released under the GPLv2 9 * This file is released under the GPLv2
8 *
9 */ 10 */
10
11#include <linux/kobject.h> 11#include <linux/kobject.h>
12#include <linux/module.h> 12#include <linux/module.h>
13#include <linux/init.h> 13#include <linux/init.h>
@@ -15,23 +15,13 @@
15 15
16#include "base.h" 16#include "base.h"
17 17
18static decl_subsys(firmware, NULL, NULL); 18struct kobject *firmware_kobj;
19 19EXPORT_SYMBOL_GPL(firmware_kobj);
20int firmware_register(struct kset *s)
21{
22 kobj_set_kset_s(s, firmware_subsys);
23 return subsystem_register(s);
24}
25
26void firmware_unregister(struct kset *s)
27{
28 subsystem_unregister(s);
29}
30 20
31int __init firmware_init(void) 21int __init firmware_init(void)
32{ 22{
33 return subsystem_register(&firmware_subsys); 23 firmware_kobj = kobject_create_and_add("firmware", NULL);
24 if (!firmware_kobj)
25 return -ENOMEM;
26 return 0;
34} 27}
35
36EXPORT_SYMBOL_GPL(firmware_register);
37EXPORT_SYMBOL_GPL(firmware_unregister);
diff --git a/drivers/base/hypervisor.c b/drivers/base/hypervisor.c
index 7080b413ddc9..6428cba3aadd 100644
--- a/drivers/base/hypervisor.c
+++ b/drivers/base/hypervisor.c
@@ -2,19 +2,23 @@
2 * hypervisor.c - /sys/hypervisor subsystem. 2 * hypervisor.c - /sys/hypervisor subsystem.
3 * 3 *
4 * Copyright (C) IBM Corp. 2006 4 * Copyright (C) IBM Corp. 2006
5 * Copyright (C) 2007 Greg Kroah-Hartman <gregkh@suse.de>
6 * Copyright (C) 2007 Novell Inc.
5 * 7 *
6 * This file is released under the GPLv2 8 * This file is released under the GPLv2
7 */ 9 */
8 10
9#include <linux/kobject.h> 11#include <linux/kobject.h>
10#include <linux/device.h> 12#include <linux/device.h>
11
12#include "base.h" 13#include "base.h"
13 14
14decl_subsys(hypervisor, NULL, NULL); 15struct kobject *hypervisor_kobj;
15EXPORT_SYMBOL_GPL(hypervisor_subsys); 16EXPORT_SYMBOL_GPL(hypervisor_kobj);
16 17
17int __init hypervisor_init(void) 18int __init hypervisor_init(void)
18{ 19{
19 return subsystem_register(&hypervisor_subsys); 20 hypervisor_kobj = kobject_create_and_add("hypervisor", NULL);
21 if (!hypervisor_kobj)
22 return -ENOMEM;
23 return 0;
20} 24}
diff --git a/drivers/base/init.c b/drivers/base/init.c
index 37138154f9e8..7bd9b6a5b01f 100644
--- a/drivers/base/init.c
+++ b/drivers/base/init.c
@@ -1,10 +1,8 @@
1/* 1/*
2 *
3 * Copyright (c) 2002-3 Patrick Mochel 2 * Copyright (c) 2002-3 Patrick Mochel
4 * Copyright (c) 2002-3 Open Source Development Labs 3 * Copyright (c) 2002-3 Open Source Development Labs
5 * 4 *
6 * This file is released under the GPLv2 5 * This file is released under the GPLv2
7 *
8 */ 6 */
9 7
10#include <linux/device.h> 8#include <linux/device.h>
@@ -14,12 +12,11 @@
14#include "base.h" 12#include "base.h"
15 13
16/** 14/**
17 * driver_init - initialize driver model. 15 * driver_init - initialize driver model.
18 * 16 *
19 * Call the driver model init functions to initialize their 17 * Call the driver model init functions to initialize their
20 * subsystems. Called early from init/main.c. 18 * subsystems. Called early from init/main.c.
21 */ 19 */
22
23void __init driver_init(void) 20void __init driver_init(void)
24{ 21{
25 /* These are the core pieces */ 22 /* These are the core pieces */
@@ -36,5 +33,4 @@ void __init driver_init(void)
36 system_bus_init(); 33 system_bus_init();
37 cpu_dev_init(); 34 cpu_dev_init();
38 memory_dev_init(); 35 memory_dev_init();
39 attribute_container_init();
40} 36}
diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 7868707c7eda..7ae413fdd5fc 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -26,7 +26,7 @@
26#define MEMORY_CLASS_NAME "memory" 26#define MEMORY_CLASS_NAME "memory"
27 27
28static struct sysdev_class memory_sysdev_class = { 28static struct sysdev_class memory_sysdev_class = {
29 set_kset_name(MEMORY_CLASS_NAME), 29 .name = MEMORY_CLASS_NAME,
30}; 30};
31 31
32static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj) 32static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
diff --git a/drivers/base/module.c b/drivers/base/module.c
new file mode 100644
index 000000000000..103be9cacb05
--- /dev/null
+++ b/drivers/base/module.c
@@ -0,0 +1,94 @@
1/*
2 * module.c - module sysfs fun for drivers
3 *
4 * This file is released under the GPLv2
5 *
6 */
7#include <linux/device.h>
8#include <linux/module.h>
9#include <linux/errno.h>
10#include <linux/string.h>
11#include "base.h"
12
13static char *make_driver_name(struct device_driver *drv)
14{
15 char *driver_name;
16
17 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
18 GFP_KERNEL);
19 if (!driver_name)
20 return NULL;
21
22 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
23 return driver_name;
24}
25
26static void module_create_drivers_dir(struct module_kobject *mk)
27{
28 if (!mk || mk->drivers_dir)
29 return;
30
31 mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj);
32}
33
34void module_add_driver(struct module *mod, struct device_driver *drv)
35{
36 char *driver_name;
37 int no_warn;
38 struct module_kobject *mk = NULL;
39
40 if (!drv)
41 return;
42
43 if (mod)
44 mk = &mod->mkobj;
45 else if (drv->mod_name) {
46 struct kobject *mkobj;
47
48 /* Lookup built-in module entry in /sys/modules */
49 mkobj = kset_find_obj(module_kset, drv->mod_name);
50 if (mkobj) {
51 mk = container_of(mkobj, struct module_kobject, kobj);
52 /* remember our module structure */
53 drv->p->mkobj = mk;
54 /* kset_find_obj took a reference */
55 kobject_put(mkobj);
56 }
57 }
58
59 if (!mk)
60 return;
61
62 /* Don't check return codes; these calls are idempotent */
63 no_warn = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module");
64 driver_name = make_driver_name(drv);
65 if (driver_name) {
66 module_create_drivers_dir(mk);
67 no_warn = sysfs_create_link(mk->drivers_dir, &drv->p->kobj,
68 driver_name);
69 kfree(driver_name);
70 }
71}
72
73void module_remove_driver(struct device_driver *drv)
74{
75 struct module_kobject *mk = NULL;
76 char *driver_name;
77
78 if (!drv)
79 return;
80
81 sysfs_remove_link(&drv->p->kobj, "module");
82
83 if (drv->owner)
84 mk = &drv->owner->mkobj;
85 else if (drv->p->mkobj)
86 mk = drv->p->mkobj;
87 if (mk && mk->drivers_dir) {
88 driver_name = make_driver_name(drv);
89 if (driver_name) {
90 sysfs_remove_link(mk->drivers_dir, driver_name);
91 kfree(driver_name);
92 }
93 }
94}
diff --git a/drivers/base/node.c b/drivers/base/node.c
index 88eeed72b5d6..e59861f18ce5 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -15,7 +15,7 @@
15#include <linux/device.h> 15#include <linux/device.h>
16 16
17static struct sysdev_class node_class = { 17static struct sysdev_class node_class = {
18 set_kset_name("node"), 18 .name = "node",
19}; 19};
20 20
21 21
diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index fb5609241482..efaf282c438c 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -20,7 +20,8 @@
20 20
21#include "base.h" 21#include "base.h"
22 22
23#define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver)) 23#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
24 driver))
24 25
25struct device platform_bus = { 26struct device platform_bus = {
26 .bus_id = "platform", 27 .bus_id = "platform",
@@ -28,14 +29,13 @@ struct device platform_bus = {
28EXPORT_SYMBOL_GPL(platform_bus); 29EXPORT_SYMBOL_GPL(platform_bus);
29 30
30/** 31/**
31 * platform_get_resource - get a resource for a device 32 * platform_get_resource - get a resource for a device
32 * @dev: platform device 33 * @dev: platform device
33 * @type: resource type 34 * @type: resource type
34 * @num: resource index 35 * @num: resource index
35 */ 36 */
36struct resource * 37struct resource *platform_get_resource(struct platform_device *dev,
37platform_get_resource(struct platform_device *dev, unsigned int type, 38 unsigned int type, unsigned int num)
38 unsigned int num)
39{ 39{
40 int i; 40 int i;
41 41
@@ -43,8 +43,7 @@ platform_get_resource(struct platform_device *dev, unsigned int type,
43 struct resource *r = &dev->resource[i]; 43 struct resource *r = &dev->resource[i];
44 44
45 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM| 45 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
46 IORESOURCE_IRQ|IORESOURCE_DMA)) 46 IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
47 == type)
48 if (num-- == 0) 47 if (num-- == 0)
49 return r; 48 return r;
50 } 49 }
@@ -53,9 +52,9 @@ platform_get_resource(struct platform_device *dev, unsigned int type,
53EXPORT_SYMBOL_GPL(platform_get_resource); 52EXPORT_SYMBOL_GPL(platform_get_resource);
54 53
55/** 54/**
56 * platform_get_irq - get an IRQ for a device 55 * platform_get_irq - get an IRQ for a device
57 * @dev: platform device 56 * @dev: platform device
58 * @num: IRQ number index 57 * @num: IRQ number index
59 */ 58 */
60int platform_get_irq(struct platform_device *dev, unsigned int num) 59int platform_get_irq(struct platform_device *dev, unsigned int num)
61{ 60{
@@ -66,14 +65,13 @@ int platform_get_irq(struct platform_device *dev, unsigned int num)
66EXPORT_SYMBOL_GPL(platform_get_irq); 65EXPORT_SYMBOL_GPL(platform_get_irq);
67 66
68/** 67/**
69 * platform_get_resource_byname - get a resource for a device by name 68 * platform_get_resource_byname - get a resource for a device by name
70 * @dev: platform device 69 * @dev: platform device
71 * @type: resource type 70 * @type: resource type
72 * @name: resource name 71 * @name: resource name
73 */ 72 */
74struct resource * 73struct resource *platform_get_resource_byname(struct platform_device *dev,
75platform_get_resource_byname(struct platform_device *dev, unsigned int type, 74 unsigned int type, char *name)
76 char *name)
77{ 75{
78 int i; 76 int i;
79 77
@@ -90,22 +88,23 @@ platform_get_resource_byname(struct platform_device *dev, unsigned int type,
90EXPORT_SYMBOL_GPL(platform_get_resource_byname); 88EXPORT_SYMBOL_GPL(platform_get_resource_byname);
91 89
92/** 90/**
93 * platform_get_irq - get an IRQ for a device 91 * platform_get_irq - get an IRQ for a device
94 * @dev: platform device 92 * @dev: platform device
95 * @name: IRQ name 93 * @name: IRQ name
96 */ 94 */
97int platform_get_irq_byname(struct platform_device *dev, char *name) 95int platform_get_irq_byname(struct platform_device *dev, char *name)
98{ 96{
99 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name); 97 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
98 name);
100 99
101 return r ? r->start : -ENXIO; 100 return r ? r->start : -ENXIO;
102} 101}
103EXPORT_SYMBOL_GPL(platform_get_irq_byname); 102EXPORT_SYMBOL_GPL(platform_get_irq_byname);
104 103
105/** 104/**
106 * platform_add_devices - add a numbers of platform devices 105 * platform_add_devices - add a numbers of platform devices
107 * @devs: array of platform devices to add 106 * @devs: array of platform devices to add
108 * @num: number of platform devices in array 107 * @num: number of platform devices in array
109 */ 108 */
110int platform_add_devices(struct platform_device **devs, int num) 109int platform_add_devices(struct platform_device **devs, int num)
111{ 110{
@@ -130,12 +129,11 @@ struct platform_object {
130}; 129};
131 130
132/** 131/**
133 * platform_device_put 132 * platform_device_put
134 * @pdev: platform device to free 133 * @pdev: platform device to free
135 * 134 *
136 * Free all memory associated with a platform device. This function 135 * Free all memory associated with a platform device. This function must
137 * must _only_ be externally called in error cases. All other usage 136 * _only_ be externally called in error cases. All other usage is a bug.
138 * is a bug.
139 */ 137 */
140void platform_device_put(struct platform_device *pdev) 138void platform_device_put(struct platform_device *pdev)
141{ 139{
@@ -146,7 +144,8 @@ EXPORT_SYMBOL_GPL(platform_device_put);
146 144
147static void platform_device_release(struct device *dev) 145static void platform_device_release(struct device *dev)
148{ 146{
149 struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev); 147 struct platform_object *pa = container_of(dev, struct platform_object,
148 pdev.dev);
150 149
151 kfree(pa->pdev.dev.platform_data); 150 kfree(pa->pdev.dev.platform_data);
152 kfree(pa->pdev.resource); 151 kfree(pa->pdev.resource);
@@ -154,12 +153,12 @@ static void platform_device_release(struct device *dev)
154} 153}
155 154
156/** 155/**
157 * platform_device_alloc 156 * platform_device_alloc
158 * @name: base name of the device we're adding 157 * @name: base name of the device we're adding
159 * @id: instance id 158 * @id: instance id
160 * 159 *
161 * Create a platform device object which can have other objects attached 160 * Create a platform device object which can have other objects attached
162 * to it, and which will have attached objects freed when it is released. 161 * to it, and which will have attached objects freed when it is released.
163 */ 162 */
164struct platform_device *platform_device_alloc(const char *name, int id) 163struct platform_device *platform_device_alloc(const char *name, int id)
165{ 164{
@@ -179,16 +178,17 @@ struct platform_device *platform_device_alloc(const char *name, int id)
179EXPORT_SYMBOL_GPL(platform_device_alloc); 178EXPORT_SYMBOL_GPL(platform_device_alloc);
180 179
181/** 180/**
182 * platform_device_add_resources 181 * platform_device_add_resources
183 * @pdev: platform device allocated by platform_device_alloc to add resources to 182 * @pdev: platform device allocated by platform_device_alloc to add resources to
184 * @res: set of resources that needs to be allocated for the device 183 * @res: set of resources that needs to be allocated for the device
185 * @num: number of resources 184 * @num: number of resources
186 * 185 *
187 * Add a copy of the resources to the platform device. The memory 186 * Add a copy of the resources to the platform device. The memory
188 * associated with the resources will be freed when the platform 187 * associated with the resources will be freed when the platform device is
189 * device is released. 188 * released.
190 */ 189 */
191int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num) 190int platform_device_add_resources(struct platform_device *pdev,
191 struct resource *res, unsigned int num)
192{ 192{
193 struct resource *r; 193 struct resource *r;
194 194
@@ -203,16 +203,17 @@ int platform_device_add_resources(struct platform_device *pdev, struct resource
203EXPORT_SYMBOL_GPL(platform_device_add_resources); 203EXPORT_SYMBOL_GPL(platform_device_add_resources);
204 204
205/** 205/**
206 * platform_device_add_data 206 * platform_device_add_data
207 * @pdev: platform device allocated by platform_device_alloc to add resources to 207 * @pdev: platform device allocated by platform_device_alloc to add resources to
208 * @data: platform specific data for this platform device 208 * @data: platform specific data for this platform device
209 * @size: size of platform specific data 209 * @size: size of platform specific data
210 * 210 *
211 * Add a copy of platform specific data to the platform device's platform_data 211 * Add a copy of platform specific data to the platform device's
212 * pointer. The memory associated with the platform data will be freed 212 * platform_data pointer. The memory associated with the platform data
213 * when the platform device is released. 213 * will be freed when the platform device is released.
214 */ 214 */
215int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size) 215int platform_device_add_data(struct platform_device *pdev, const void *data,
216 size_t size)
216{ 217{
217 void *d; 218 void *d;
218 219
@@ -226,11 +227,11 @@ int platform_device_add_data(struct platform_device *pdev, const void *data, siz
226EXPORT_SYMBOL_GPL(platform_device_add_data); 227EXPORT_SYMBOL_GPL(platform_device_add_data);
227 228
228/** 229/**
229 * platform_device_add - add a platform device to device hierarchy 230 * platform_device_add - add a platform device to device hierarchy
230 * @pdev: platform device we're adding 231 * @pdev: platform device we're adding
231 * 232 *
232 * This is part 2 of platform_device_register(), though may be called 233 * This is part 2 of platform_device_register(), though may be called
233 * separately _iff_ pdev was allocated by platform_device_alloc(). 234 * separately _iff_ pdev was allocated by platform_device_alloc().
234 */ 235 */
235int platform_device_add(struct platform_device *pdev) 236int platform_device_add(struct platform_device *pdev)
236{ 237{
@@ -289,13 +290,12 @@ int platform_device_add(struct platform_device *pdev)
289EXPORT_SYMBOL_GPL(platform_device_add); 290EXPORT_SYMBOL_GPL(platform_device_add);
290 291
291/** 292/**
292 * platform_device_del - remove a platform-level device 293 * platform_device_del - remove a platform-level device
293 * @pdev: platform device we're removing 294 * @pdev: platform device we're removing
294 * 295 *
295 * Note that this function will also release all memory- and port-based 296 * Note that this function will also release all memory- and port-based
296 * resources owned by the device (@dev->resource). This function 297 * resources owned by the device (@dev->resource). This function must
297 * must _only_ be externally called in error cases. All other usage 298 * _only_ be externally called in error cases. All other usage is a bug.
298 * is a bug.
299 */ 299 */
300void platform_device_del(struct platform_device *pdev) 300void platform_device_del(struct platform_device *pdev)
301{ 301{
@@ -314,11 +314,10 @@ void platform_device_del(struct platform_device *pdev)
314EXPORT_SYMBOL_GPL(platform_device_del); 314EXPORT_SYMBOL_GPL(platform_device_del);
315 315
316/** 316/**
317 * platform_device_register - add a platform-level device 317 * platform_device_register - add a platform-level device
318 * @pdev: platform device we're adding 318 * @pdev: platform device we're adding
319 *
320 */ 319 */
321int platform_device_register(struct platform_device * pdev) 320int platform_device_register(struct platform_device *pdev)
322{ 321{
323 device_initialize(&pdev->dev); 322 device_initialize(&pdev->dev);
324 return platform_device_add(pdev); 323 return platform_device_add(pdev);
@@ -326,14 +325,14 @@ int platform_device_register(struct platform_device * pdev)
326EXPORT_SYMBOL_GPL(platform_device_register); 325EXPORT_SYMBOL_GPL(platform_device_register);
327 326
328/** 327/**
329 * platform_device_unregister - unregister a platform-level device 328 * platform_device_unregister - unregister a platform-level device
330 * @pdev: platform device we're unregistering 329 * @pdev: platform device we're unregistering
331 * 330 *
332 * Unregistration is done in 2 steps. First we release all resources 331 * Unregistration is done in 2 steps. First we release all resources
333 * and remove it from the subsystem, then we drop reference count by 332 * and remove it from the subsystem, then we drop reference count by
334 * calling platform_device_put(). 333 * calling platform_device_put().
335 */ 334 */
336void platform_device_unregister(struct platform_device * pdev) 335void platform_device_unregister(struct platform_device *pdev)
337{ 336{
338 platform_device_del(pdev); 337 platform_device_del(pdev);
339 platform_device_put(pdev); 338 platform_device_put(pdev);
@@ -341,27 +340,29 @@ void platform_device_unregister(struct platform_device * pdev)
341EXPORT_SYMBOL_GPL(platform_device_unregister); 340EXPORT_SYMBOL_GPL(platform_device_unregister);
342 341
343/** 342/**
344 * platform_device_register_simple 343 * platform_device_register_simple
345 * @name: base name of the device we're adding 344 * @name: base name of the device we're adding
346 * @id: instance id 345 * @id: instance id
347 * @res: set of resources that needs to be allocated for the device 346 * @res: set of resources that needs to be allocated for the device
348 * @num: number of resources 347 * @num: number of resources
349 * 348 *
350 * This function creates a simple platform device that requires minimal 349 * This function creates a simple platform device that requires minimal
351 * resource and memory management. Canned release function freeing 350 * resource and memory management. Canned release function freeing memory
352 * memory allocated for the device allows drivers using such devices 351 * allocated for the device allows drivers using such devices to be
353 * to be unloaded without waiting for the last reference to the device 352 * unloaded without waiting for the last reference to the device to be
354 * to be dropped. 353 * dropped.
355 * 354 *
356 * This interface is primarily intended for use with legacy drivers 355 * This interface is primarily intended for use with legacy drivers which
357 * which probe hardware directly. Because such drivers create sysfs 356 * probe hardware directly. Because such drivers create sysfs device nodes
358 * device nodes themselves, rather than letting system infrastructure 357 * themselves, rather than letting system infrastructure handle such device
359 * handle such device enumeration tasks, they don't fully conform to 358 * enumeration tasks, they don't fully conform to the Linux driver model.
360 * the Linux driver model. In particular, when such drivers are built 359 * In particular, when such drivers are built as modules, they can't be
361 * as modules, they can't be "hotplugged". 360 * "hotplugged".
362 */ 361 */
363struct platform_device *platform_device_register_simple(char *name, int id, 362struct platform_device *platform_device_register_simple(const char *name,
364 struct resource *res, unsigned int num) 363 int id,
364 struct resource *res,
365 unsigned int num)
365{ 366{
366 struct platform_device *pdev; 367 struct platform_device *pdev;
367 int retval; 368 int retval;
@@ -436,8 +437,8 @@ static int platform_drv_resume(struct device *_dev)
436} 437}
437 438
438/** 439/**
439 * platform_driver_register 440 * platform_driver_register
440 * @drv: platform driver structure 441 * @drv: platform driver structure
441 */ 442 */
442int platform_driver_register(struct platform_driver *drv) 443int platform_driver_register(struct platform_driver *drv)
443{ 444{
@@ -457,8 +458,8 @@ int platform_driver_register(struct platform_driver *drv)
457EXPORT_SYMBOL_GPL(platform_driver_register); 458EXPORT_SYMBOL_GPL(platform_driver_register);
458 459
459/** 460/**
460 * platform_driver_unregister 461 * platform_driver_unregister
461 * @drv: platform driver structure 462 * @drv: platform driver structure
462 */ 463 */
463void platform_driver_unregister(struct platform_driver *drv) 464void platform_driver_unregister(struct platform_driver *drv)
464{ 465{
@@ -497,12 +498,12 @@ int __init_or_module platform_driver_probe(struct platform_driver *drv,
497 * if the probe was successful, and make sure any forced probes of 498 * if the probe was successful, and make sure any forced probes of
498 * new devices fail. 499 * new devices fail.
499 */ 500 */
500 spin_lock(&platform_bus_type.klist_drivers.k_lock); 501 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
501 drv->probe = NULL; 502 drv->probe = NULL;
502 if (code == 0 && list_empty(&drv->driver.klist_devices.k_list)) 503 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
503 retval = -ENODEV; 504 retval = -ENODEV;
504 drv->driver.probe = platform_drv_probe_fail; 505 drv->driver.probe = platform_drv_probe_fail;
505 spin_unlock(&platform_bus_type.klist_drivers.k_lock); 506 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
506 507
507 if (code != retval) 508 if (code != retval)
508 platform_driver_unregister(drv); 509 platform_driver_unregister(drv);
@@ -516,8 +517,8 @@ EXPORT_SYMBOL_GPL(platform_driver_probe);
516 * (b) sysfs attribute lets new-style coldplug recover from hotplug events 517 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
517 * mishandled before system is fully running: "modprobe $(cat modalias)" 518 * mishandled before system is fully running: "modprobe $(cat modalias)"
518 */ 519 */
519static ssize_t 520static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
520modalias_show(struct device *dev, struct device_attribute *a, char *buf) 521 char *buf)
521{ 522{
522 struct platform_device *pdev = to_platform_device(dev); 523 struct platform_device *pdev = to_platform_device(dev);
523 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); 524 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
@@ -538,26 +539,24 @@ static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
538 return 0; 539 return 0;
539} 540}
540 541
541
542/** 542/**
543 * platform_match - bind platform device to platform driver. 543 * platform_match - bind platform device to platform driver.
544 * @dev: device. 544 * @dev: device.
545 * @drv: driver. 545 * @drv: driver.
546 * 546 *
547 * Platform device IDs are assumed to be encoded like this: 547 * Platform device IDs are assumed to be encoded like this:
548 * "<name><instance>", where <name> is a short description of the 548 * "<name><instance>", where <name> is a short description of the type of
549 * type of device, like "pci" or "floppy", and <instance> is the 549 * device, like "pci" or "floppy", and <instance> is the enumerated
550 * enumerated instance of the device, like '0' or '42'. 550 * instance of the device, like '0' or '42'. Driver IDs are simply
551 * Driver IDs are simply "<name>". 551 * "<name>". So, extract the <name> from the platform_device structure,
552 * So, extract the <name> from the platform_device structure, 552 * and compare it against the name of the driver. Return whether they match
553 * and compare it against the name of the driver. Return whether 553 * or not.
554 * they match or not.
555 */ 554 */
556 555static int platform_match(struct device *dev, struct device_driver *drv)
557static int platform_match(struct device * dev, struct device_driver * drv)
558{ 556{
559 struct platform_device *pdev = container_of(dev, struct platform_device, dev); 557 struct platform_device *pdev;
560 558
559 pdev = container_of(dev, struct platform_device, dev);
561 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); 560 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
562} 561}
563 562
@@ -574,9 +573,10 @@ static int platform_suspend(struct device *dev, pm_message_t mesg)
574static int platform_suspend_late(struct device *dev, pm_message_t mesg) 573static int platform_suspend_late(struct device *dev, pm_message_t mesg)
575{ 574{
576 struct platform_driver *drv = to_platform_driver(dev->driver); 575 struct platform_driver *drv = to_platform_driver(dev->driver);
577 struct platform_device *pdev = container_of(dev, struct platform_device, dev); 576 struct platform_device *pdev;
578 int ret = 0; 577 int ret = 0;
579 578
579 pdev = container_of(dev, struct platform_device, dev);
580 if (dev->driver && drv->suspend_late) 580 if (dev->driver && drv->suspend_late)
581 ret = drv->suspend_late(pdev, mesg); 581 ret = drv->suspend_late(pdev, mesg);
582 582
@@ -586,16 +586,17 @@ static int platform_suspend_late(struct device *dev, pm_message_t mesg)
586static int platform_resume_early(struct device *dev) 586static int platform_resume_early(struct device *dev)
587{ 587{
588 struct platform_driver *drv = to_platform_driver(dev->driver); 588 struct platform_driver *drv = to_platform_driver(dev->driver);
589 struct platform_device *pdev = container_of(dev, struct platform_device, dev); 589 struct platform_device *pdev;
590 int ret = 0; 590 int ret = 0;
591 591
592 pdev = container_of(dev, struct platform_device, dev);
592 if (dev->driver && drv->resume_early) 593 if (dev->driver && drv->resume_early)
593 ret = drv->resume_early(pdev); 594 ret = drv->resume_early(pdev);
594 595
595 return ret; 596 return ret;
596} 597}
597 598
598static int platform_resume(struct device * dev) 599static int platform_resume(struct device *dev)
599{ 600{
600 int ret = 0; 601 int ret = 0;
601 602
diff --git a/drivers/base/power/Makefile b/drivers/base/power/Makefile
index 44504e6618fb..06a86fe6a78d 100644
--- a/drivers/base/power/Makefile
+++ b/drivers/base/power/Makefile
@@ -1,4 +1,3 @@
1obj-y := shutdown.o
2obj-$(CONFIG_PM) += sysfs.o 1obj-$(CONFIG_PM) += sysfs.o
3obj-$(CONFIG_PM_SLEEP) += main.o 2obj-$(CONFIG_PM_SLEEP) += main.o
4obj-$(CONFIG_PM_TRACE) += trace.o 3obj-$(CONFIG_PM_TRACE) += trace.o
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index 691ffb64cc37..200ed5fafd50 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -24,20 +24,45 @@
24#include <linux/mutex.h> 24#include <linux/mutex.h>
25#include <linux/pm.h> 25#include <linux/pm.h>
26#include <linux/resume-trace.h> 26#include <linux/resume-trace.h>
27#include <linux/rwsem.h>
27 28
28#include "../base.h" 29#include "../base.h"
29#include "power.h" 30#include "power.h"
30 31
32/*
33 * The entries in the dpm_active list are in a depth first order, simply
34 * because children are guaranteed to be discovered after parents, and
35 * are inserted at the back of the list on discovery.
36 *
37 * All the other lists are kept in the same order, for consistency.
38 * However the lists aren't always traversed in the same order.
39 * Semaphores must be acquired from the top (i.e., front) down
40 * and released in the opposite order. Devices must be suspended
41 * from the bottom (i.e., end) up and resumed in the opposite order.
42 * That way no parent will be suspended while it still has an active
43 * child.
44 *
45 * Since device_pm_add() may be called with a device semaphore held,
46 * we must never try to acquire a device semaphore while holding
47 * dpm_list_mutex.
48 */
49
31LIST_HEAD(dpm_active); 50LIST_HEAD(dpm_active);
51static LIST_HEAD(dpm_locked);
32static LIST_HEAD(dpm_off); 52static LIST_HEAD(dpm_off);
33static LIST_HEAD(dpm_off_irq); 53static LIST_HEAD(dpm_off_irq);
54static LIST_HEAD(dpm_destroy);
34 55
35static DEFINE_MUTEX(dpm_mtx);
36static DEFINE_MUTEX(dpm_list_mtx); 56static DEFINE_MUTEX(dpm_list_mtx);
37 57
38int (*platform_enable_wakeup)(struct device *dev, int is_on); 58static DECLARE_RWSEM(pm_sleep_rwsem);
39 59
60int (*platform_enable_wakeup)(struct device *dev, int is_on);
40 61
62/**
63 * device_pm_add - add a device to the list of active devices
64 * @dev: Device to be added to the list
65 */
41void device_pm_add(struct device *dev) 66void device_pm_add(struct device *dev)
42{ 67{
43 pr_debug("PM: Adding info for %s:%s\n", 68 pr_debug("PM: Adding info for %s:%s\n",
@@ -48,8 +73,36 @@ void device_pm_add(struct device *dev)
48 mutex_unlock(&dpm_list_mtx); 73 mutex_unlock(&dpm_list_mtx);
49} 74}
50 75
76/**
77 * device_pm_remove - remove a device from the list of active devices
78 * @dev: Device to be removed from the list
79 *
80 * This function also removes the device's PM-related sysfs attributes.
81 */
51void device_pm_remove(struct device *dev) 82void device_pm_remove(struct device *dev)
52{ 83{
84 /*
85 * If this function is called during a suspend, it will be blocked,
86 * because we're holding the device's semaphore at that time, which may
87 * lead to a deadlock. In that case we want to print a warning.
88 * However, it may also be called by unregister_dropped_devices() with
89 * the device's semaphore released, in which case the warning should
90 * not be printed.
91 */
92 if (down_trylock(&dev->sem)) {
93 if (down_read_trylock(&pm_sleep_rwsem)) {
94 /* No suspend in progress, wait on dev->sem */
95 down(&dev->sem);
96 up_read(&pm_sleep_rwsem);
97 } else {
98 /* Suspend in progress, we may deadlock */
99 dev_warn(dev, "Suspicious %s during suspend\n",
100 __FUNCTION__);
101 dump_stack();
102 /* The user has been warned ... */
103 down(&dev->sem);
104 }
105 }
53 pr_debug("PM: Removing info for %s:%s\n", 106 pr_debug("PM: Removing info for %s:%s\n",
54 dev->bus ? dev->bus->name : "No Bus", 107 dev->bus ? dev->bus->name : "No Bus",
55 kobject_name(&dev->kobj)); 108 kobject_name(&dev->kobj));
@@ -57,25 +110,124 @@ void device_pm_remove(struct device *dev)
57 dpm_sysfs_remove(dev); 110 dpm_sysfs_remove(dev);
58 list_del_init(&dev->power.entry); 111 list_del_init(&dev->power.entry);
59 mutex_unlock(&dpm_list_mtx); 112 mutex_unlock(&dpm_list_mtx);
113 up(&dev->sem);
114}
115
116/**
117 * device_pm_schedule_removal - schedule the removal of a suspended device
118 * @dev: Device to destroy
119 *
120 * Moves the device to the dpm_destroy list for further processing by
121 * unregister_dropped_devices().
122 */
123void device_pm_schedule_removal(struct device *dev)
124{
125 pr_debug("PM: Preparing for removal: %s:%s\n",
126 dev->bus ? dev->bus->name : "No Bus",
127 kobject_name(&dev->kobj));
128 mutex_lock(&dpm_list_mtx);
129 list_move_tail(&dev->power.entry, &dpm_destroy);
130 mutex_unlock(&dpm_list_mtx);
131}
132
133/**
134 * pm_sleep_lock - mutual exclusion for registration and suspend
135 *
136 * Returns 0 if no suspend is underway and device registration
137 * may proceed, otherwise -EBUSY.
138 */
139int pm_sleep_lock(void)
140{
141 if (down_read_trylock(&pm_sleep_rwsem))
142 return 0;
143
144 return -EBUSY;
145}
146
147/**
148 * pm_sleep_unlock - mutual exclusion for registration and suspend
149 *
150 * This routine undoes the effect of device_pm_add_lock
151 * when a device's registration is complete.
152 */
153void pm_sleep_unlock(void)
154{
155 up_read(&pm_sleep_rwsem);
60} 156}
61 157
62 158
63/*------------------------- Resume routines -------------------------*/ 159/*------------------------- Resume routines -------------------------*/
64 160
65/** 161/**
66 * resume_device - Restore state for one device. 162 * resume_device_early - Power on one device (early resume).
67 * @dev: Device. 163 * @dev: Device.
68 * 164 *
165 * Must be called with interrupts disabled.
69 */ 166 */
70 167static int resume_device_early(struct device *dev)
71static int resume_device(struct device * dev)
72{ 168{
73 int error = 0; 169 int error = 0;
74 170
75 TRACE_DEVICE(dev); 171 TRACE_DEVICE(dev);
76 TRACE_RESUME(0); 172 TRACE_RESUME(0);
77 173
78 down(&dev->sem); 174 if (dev->bus && dev->bus->resume_early) {
175 dev_dbg(dev, "EARLY resume\n");
176 error = dev->bus->resume_early(dev);
177 }
178
179 TRACE_RESUME(error);
180 return error;
181}
182
183/**
184 * dpm_power_up - Power on all regular (non-sysdev) devices.
185 *
186 * Walk the dpm_off_irq list and power each device up. This
187 * is used for devices that required they be powered down with
188 * interrupts disabled. As devices are powered on, they are moved
189 * to the dpm_off list.
190 *
191 * Must be called with interrupts disabled and only one CPU running.
192 */
193static void dpm_power_up(void)
194{
195
196 while (!list_empty(&dpm_off_irq)) {
197 struct list_head *entry = dpm_off_irq.next;
198 struct device *dev = to_device(entry);
199
200 list_move_tail(entry, &dpm_off);
201 resume_device_early(dev);
202 }
203}
204
205/**
206 * device_power_up - Turn on all devices that need special attention.
207 *
208 * Power on system devices, then devices that required we shut them down
209 * with interrupts disabled.
210 *
211 * Must be called with interrupts disabled.
212 */
213void device_power_up(void)
214{
215 sysdev_resume();
216 dpm_power_up();
217}
218EXPORT_SYMBOL_GPL(device_power_up);
219
220/**
221 * resume_device - Restore state for one device.
222 * @dev: Device.
223 *
224 */
225static int resume_device(struct device *dev)
226{
227 int error = 0;
228
229 TRACE_DEVICE(dev);
230 TRACE_RESUME(0);
79 231
80 if (dev->bus && dev->bus->resume) { 232 if (dev->bus && dev->bus->resume) {
81 dev_dbg(dev,"resuming\n"); 233 dev_dbg(dev,"resuming\n");
@@ -92,126 +244,94 @@ static int resume_device(struct device * dev)
92 error = dev->class->resume(dev); 244 error = dev->class->resume(dev);
93 } 245 }
94 246
95 up(&dev->sem);
96
97 TRACE_RESUME(error); 247 TRACE_RESUME(error);
98 return error; 248 return error;
99} 249}
100 250
101 251/**
102static int resume_device_early(struct device * dev) 252 * dpm_resume - Resume every device.
103{ 253 *
104 int error = 0; 254 * Resume the devices that have either not gone through
105 255 * the late suspend, or that did go through it but also
106 TRACE_DEVICE(dev); 256 * went through the early resume.
107 TRACE_RESUME(0); 257 *
108 if (dev->bus && dev->bus->resume_early) { 258 * Take devices from the dpm_off_list, resume them,
109 dev_dbg(dev,"EARLY resume\n"); 259 * and put them on the dpm_locked list.
110 error = dev->bus->resume_early(dev);
111 }
112 TRACE_RESUME(error);
113 return error;
114}
115
116/*
117 * Resume the devices that have either not gone through
118 * the late suspend, or that did go through it but also
119 * went through the early resume
120 */ 260 */
121static void dpm_resume(void) 261static void dpm_resume(void)
122{ 262{
123 mutex_lock(&dpm_list_mtx); 263 mutex_lock(&dpm_list_mtx);
124 while(!list_empty(&dpm_off)) { 264 while(!list_empty(&dpm_off)) {
125 struct list_head * entry = dpm_off.next; 265 struct list_head *entry = dpm_off.next;
126 struct device * dev = to_device(entry); 266 struct device *dev = to_device(entry);
127
128 get_device(dev);
129 list_move_tail(entry, &dpm_active);
130 267
268 list_move_tail(entry, &dpm_locked);
131 mutex_unlock(&dpm_list_mtx); 269 mutex_unlock(&dpm_list_mtx);
132 resume_device(dev); 270 resume_device(dev);
133 mutex_lock(&dpm_list_mtx); 271 mutex_lock(&dpm_list_mtx);
134 put_device(dev);
135 } 272 }
136 mutex_unlock(&dpm_list_mtx); 273 mutex_unlock(&dpm_list_mtx);
137} 274}
138 275
139
140/** 276/**
141 * device_resume - Restore state of each device in system. 277 * unlock_all_devices - Release each device's semaphore
142 * 278 *
143 * Walk the dpm_off list, remove each entry, resume the device, 279 * Go through the dpm_off list. Put each device on the dpm_active
144 * then add it to the dpm_active list. 280 * list and unlock it.
145 */ 281 */
146 282static void unlock_all_devices(void)
147void device_resume(void)
148{ 283{
149 might_sleep(); 284 mutex_lock(&dpm_list_mtx);
150 mutex_lock(&dpm_mtx); 285 while (!list_empty(&dpm_locked)) {
151 dpm_resume(); 286 struct list_head *entry = dpm_locked.prev;
152 mutex_unlock(&dpm_mtx); 287 struct device *dev = to_device(entry);
153}
154
155EXPORT_SYMBOL_GPL(device_resume);
156 288
289 list_move(entry, &dpm_active);
290 up(&dev->sem);
291 }
292 mutex_unlock(&dpm_list_mtx);
293}
157 294
158/** 295/**
159 * dpm_power_up - Power on some devices. 296 * unregister_dropped_devices - Unregister devices scheduled for removal
160 *
161 * Walk the dpm_off_irq list and power each device up. This
162 * is used for devices that required they be powered down with
163 * interrupts disabled. As devices are powered on, they are moved
164 * to the dpm_active list.
165 * 297 *
166 * Interrupts must be disabled when calling this. 298 * Unregister all devices on the dpm_destroy list.
167 */ 299 */
168 300static void unregister_dropped_devices(void)
169static void dpm_power_up(void)
170{ 301{
171 while(!list_empty(&dpm_off_irq)) { 302 mutex_lock(&dpm_list_mtx);
172 struct list_head * entry = dpm_off_irq.next; 303 while (!list_empty(&dpm_destroy)) {
173 struct device * dev = to_device(entry); 304 struct list_head *entry = dpm_destroy.next;
305 struct device *dev = to_device(entry);
174 306
175 list_move_tail(entry, &dpm_off); 307 up(&dev->sem);
176 resume_device_early(dev); 308 mutex_unlock(&dpm_list_mtx);
309 /* This also removes the device from the list */
310 device_unregister(dev);
311 mutex_lock(&dpm_list_mtx);
177 } 312 }
313 mutex_unlock(&dpm_list_mtx);
178} 314}
179 315
180
181/** 316/**
182 * device_power_up - Turn on all devices that need special attention. 317 * device_resume - Restore state of each device in system.
183 * 318 *
184 * Power on system devices then devices that required we shut them down 319 * Resume all the devices, unlock them all, and allow new
185 * with interrupts disabled. 320 * devices to be registered once again.
186 * Called with interrupts disabled.
187 */ 321 */
188 322void device_resume(void)
189void device_power_up(void)
190{ 323{
191 sysdev_resume(); 324 might_sleep();
192 dpm_power_up(); 325 dpm_resume();
326 unlock_all_devices();
327 unregister_dropped_devices();
328 up_write(&pm_sleep_rwsem);
193} 329}
194 330EXPORT_SYMBOL_GPL(device_resume);
195EXPORT_SYMBOL_GPL(device_power_up);
196 331
197 332
198/*------------------------- Suspend routines -------------------------*/ 333/*------------------------- Suspend routines -------------------------*/
199 334
200/*
201 * The entries in the dpm_active list are in a depth first order, simply
202 * because children are guaranteed to be discovered after parents, and
203 * are inserted at the back of the list on discovery.
204 *
205 * All list on the suspend path are done in reverse order, so we operate
206 * on the leaves of the device tree (or forests, depending on how you want
207 * to look at it ;) first. As nodes are removed from the back of the list,
208 * they are inserted into the front of their destintation lists.
209 *
210 * Things are the reverse on the resume path - iterations are done in
211 * forward order, and nodes are inserted at the back of their destination
212 * lists. This way, the ancestors will be accessed before their descendents.
213 */
214
215static inline char *suspend_verb(u32 event) 335static inline char *suspend_verb(u32 event)
216{ 336{
217 switch (event) { 337 switch (event) {
@@ -222,7 +342,6 @@ static inline char *suspend_verb(u32 event)
222 } 342 }
223} 343}
224 344
225
226static void 345static void
227suspend_device_dbg(struct device *dev, pm_message_t state, char *info) 346suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
228{ 347{
@@ -232,16 +351,73 @@ suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
232} 351}
233 352
234/** 353/**
235 * suspend_device - Save state of one device. 354 * suspend_device_late - Shut down one device (late suspend).
236 * @dev: Device. 355 * @dev: Device.
237 * @state: Power state device is entering. 356 * @state: Power state device is entering.
357 *
358 * This is called with interrupts off and only a single CPU running.
238 */ 359 */
360static int suspend_device_late(struct device *dev, pm_message_t state)
361{
362 int error = 0;
239 363
240static int suspend_device(struct device * dev, pm_message_t state) 364 if (dev->bus && dev->bus->suspend_late) {
365 suspend_device_dbg(dev, state, "LATE ");
366 error = dev->bus->suspend_late(dev, state);
367 suspend_report_result(dev->bus->suspend_late, error);
368 }
369 return error;
370}
371
372/**
373 * device_power_down - Shut down special devices.
374 * @state: Power state to enter.
375 *
376 * Power down devices that require interrupts to be disabled
377 * and move them from the dpm_off list to the dpm_off_irq list.
378 * Then power down system devices.
379 *
380 * Must be called with interrupts disabled and only one CPU running.
381 */
382int device_power_down(pm_message_t state)
383{
384 int error = 0;
385
386 while (!list_empty(&dpm_off)) {
387 struct list_head *entry = dpm_off.prev;
388 struct device *dev = to_device(entry);
389
390 list_del_init(&dev->power.entry);
391 error = suspend_device_late(dev, state);
392 if (error) {
393 printk(KERN_ERR "Could not power down device %s: "
394 "error %d\n",
395 kobject_name(&dev->kobj), error);
396 if (list_empty(&dev->power.entry))
397 list_add(&dev->power.entry, &dpm_off);
398 break;
399 }
400 if (list_empty(&dev->power.entry))
401 list_add(&dev->power.entry, &dpm_off_irq);
402 }
403
404 if (!error)
405 error = sysdev_suspend(state);
406 if (error)
407 dpm_power_up();
408 return error;
409}
410EXPORT_SYMBOL_GPL(device_power_down);
411
412/**
413 * suspend_device - Save state of one device.
414 * @dev: Device.
415 * @state: Power state device is entering.
416 */
417int suspend_device(struct device *dev, pm_message_t state)
241{ 418{
242 int error = 0; 419 int error = 0;
243 420
244 down(&dev->sem);
245 if (dev->power.power_state.event) { 421 if (dev->power.power_state.event) {
246 dev_dbg(dev, "PM: suspend %d-->%d\n", 422 dev_dbg(dev, "PM: suspend %d-->%d\n",
247 dev->power.power_state.event, state.event); 423 dev->power.power_state.event, state.event);
@@ -264,123 +440,105 @@ static int suspend_device(struct device * dev, pm_message_t state)
264 error = dev->bus->suspend(dev, state); 440 error = dev->bus->suspend(dev, state);
265 suspend_report_result(dev->bus->suspend, error); 441 suspend_report_result(dev->bus->suspend, error);
266 } 442 }
267 up(&dev->sem);
268 return error;
269}
270
271
272/*
273 * This is called with interrupts off, only a single CPU
274 * running. We can't acquire a mutex or semaphore (and we don't
275 * need the protection)
276 */
277static int suspend_device_late(struct device *dev, pm_message_t state)
278{
279 int error = 0;
280
281 if (dev->bus && dev->bus->suspend_late) {
282 suspend_device_dbg(dev, state, "LATE ");
283 error = dev->bus->suspend_late(dev, state);
284 suspend_report_result(dev->bus->suspend_late, error);
285 }
286 return error; 443 return error;
287} 444}
288 445
289/** 446/**
290 * device_suspend - Save state and stop all devices in system. 447 * dpm_suspend - Suspend every device.
291 * @state: Power state to put each device in. 448 * @state: Power state to put each device in.
292 * 449 *
293 * Walk the dpm_active list, call ->suspend() for each device, and move 450 * Walk the dpm_locked list. Suspend each device and move it
294 * it to the dpm_off list. 451 * to the dpm_off list.
295 * 452 *
296 * (For historical reasons, if it returns -EAGAIN, that used to mean 453 * (For historical reasons, if it returns -EAGAIN, that used to mean
297 * that the device would be called again with interrupts disabled. 454 * that the device would be called again with interrupts disabled.
298 * These days, we use the "suspend_late()" callback for that, so we 455 * These days, we use the "suspend_late()" callback for that, so we
299 * print a warning and consider it an error). 456 * print a warning and consider it an error).
300 *
301 * If we get a different error, try and back out.
302 *
303 * If we hit a failure with any of the devices, call device_resume()
304 * above to bring the suspended devices back to life.
305 *
306 */ 457 */
307 458static int dpm_suspend(pm_message_t state)
308int device_suspend(pm_message_t state)
309{ 459{
310 int error = 0; 460 int error = 0;
311 461
312 might_sleep();
313 mutex_lock(&dpm_mtx);
314 mutex_lock(&dpm_list_mtx); 462 mutex_lock(&dpm_list_mtx);
315 while (!list_empty(&dpm_active) && error == 0) { 463 while (!list_empty(&dpm_locked)) {
316 struct list_head * entry = dpm_active.prev; 464 struct list_head *entry = dpm_locked.prev;
317 struct device * dev = to_device(entry); 465 struct device *dev = to_device(entry);
318 466
319 get_device(dev); 467 list_del_init(&dev->power.entry);
320 mutex_unlock(&dpm_list_mtx); 468 mutex_unlock(&dpm_list_mtx);
321
322 error = suspend_device(dev, state); 469 error = suspend_device(dev, state);
323 470 if (error) {
324 mutex_lock(&dpm_list_mtx);
325
326 /* Check if the device got removed */
327 if (!list_empty(&dev->power.entry)) {
328 /* Move it to the dpm_off list */
329 if (!error)
330 list_move(&dev->power.entry, &dpm_off);
331 }
332 if (error)
333 printk(KERN_ERR "Could not suspend device %s: " 471 printk(KERN_ERR "Could not suspend device %s: "
334 "error %d%s\n", 472 "error %d%s\n",
335 kobject_name(&dev->kobj), error, 473 kobject_name(&dev->kobj),
336 error == -EAGAIN ? " (please convert to suspend_late)" : ""); 474 error,
337 put_device(dev); 475 (error == -EAGAIN ?
476 " (please convert to suspend_late)" :
477 ""));
478 mutex_lock(&dpm_list_mtx);
479 if (list_empty(&dev->power.entry))
480 list_add(&dev->power.entry, &dpm_locked);
481 mutex_unlock(&dpm_list_mtx);
482 break;
483 }
484 mutex_lock(&dpm_list_mtx);
485 if (list_empty(&dev->power.entry))
486 list_add(&dev->power.entry, &dpm_off);
338 } 487 }
339 mutex_unlock(&dpm_list_mtx); 488 mutex_unlock(&dpm_list_mtx);
340 if (error)
341 dpm_resume();
342 489
343 mutex_unlock(&dpm_mtx);
344 return error; 490 return error;
345} 491}
346 492
347EXPORT_SYMBOL_GPL(device_suspend);
348
349/** 493/**
350 * device_power_down - Shut down special devices. 494 * lock_all_devices - Acquire every device's semaphore
351 * @state: Power state to enter.
352 * 495 *
353 * Walk the dpm_off_irq list, calling ->power_down() for each device that 496 * Go through the dpm_active list. Carefully lock each device's
354 * couldn't power down the device with interrupts enabled. When we're 497 * semaphore and put it in on the dpm_locked list.
355 * done, power down system devices.
356 */ 498 */
357 499static void lock_all_devices(void)
358int device_power_down(pm_message_t state)
359{ 500{
360 int error = 0; 501 mutex_lock(&dpm_list_mtx);
361 struct device * dev; 502 while (!list_empty(&dpm_active)) {
503 struct list_head *entry = dpm_active.next;
504 struct device *dev = to_device(entry);
362 505
363 while (!list_empty(&dpm_off)) { 506 /* Required locking order is dev->sem first,
364 struct list_head * entry = dpm_off.prev; 507 * then dpm_list_mutex. Hence this awkward code.
508 */
509 get_device(dev);
510 mutex_unlock(&dpm_list_mtx);
511 down(&dev->sem);
512 mutex_lock(&dpm_list_mtx);
365 513
366 dev = to_device(entry); 514 if (list_empty(entry))
367 error = suspend_device_late(dev, state); 515 up(&dev->sem); /* Device was removed */
368 if (error) 516 else
369 goto Error; 517 list_move_tail(entry, &dpm_locked);
370 list_move(&dev->power.entry, &dpm_off_irq); 518 put_device(dev);
371 } 519 }
520 mutex_unlock(&dpm_list_mtx);
521}
522
523/**
524 * device_suspend - Save state and stop all devices in system.
525 *
526 * Prevent new devices from being registered, then lock all devices
527 * and suspend them.
528 */
529int device_suspend(pm_message_t state)
530{
531 int error;
372 532
373 error = sysdev_suspend(state); 533 might_sleep();
374 Done: 534 down_write(&pm_sleep_rwsem);
535 lock_all_devices();
536 error = dpm_suspend(state);
537 if (error)
538 device_resume();
375 return error; 539 return error;
376 Error:
377 printk(KERN_ERR "Could not power down device %s: "
378 "error %d\n", kobject_name(&dev->kobj), error);
379 dpm_power_up();
380 goto Done;
381} 540}
382 541EXPORT_SYMBOL_GPL(device_suspend);
383EXPORT_SYMBOL_GPL(device_power_down);
384 542
385void __suspend_report_result(const char *function, void *fn, int ret) 543void __suspend_report_result(const char *function, void *fn, int ret)
386{ 544{
diff --git a/drivers/base/power/power.h b/drivers/base/power/power.h
index 379da4e958e0..6f0dfca8ebdd 100644
--- a/drivers/base/power/power.h
+++ b/drivers/base/power/power.h
@@ -1,10 +1,3 @@
1/*
2 * shutdown.c
3 */
4
5extern void device_shutdown(void);
6
7
8#ifdef CONFIG_PM_SLEEP 1#ifdef CONFIG_PM_SLEEP
9 2
10/* 3/*
@@ -20,6 +13,9 @@ static inline struct device *to_device(struct list_head *entry)
20 13
21extern void device_pm_add(struct device *); 14extern void device_pm_add(struct device *);
22extern void device_pm_remove(struct device *); 15extern void device_pm_remove(struct device *);
16extern void device_pm_schedule_removal(struct device *);
17extern int pm_sleep_lock(void);
18extern void pm_sleep_unlock(void);
23 19
24#else /* CONFIG_PM_SLEEP */ 20#else /* CONFIG_PM_SLEEP */
25 21
@@ -32,6 +28,15 @@ static inline void device_pm_remove(struct device *dev)
32{ 28{
33} 29}
34 30
31static inline int pm_sleep_lock(void)
32{
33 return 0;
34}
35
36static inline void pm_sleep_unlock(void)
37{
38}
39
35#endif 40#endif
36 41
37#ifdef CONFIG_PM 42#ifdef CONFIG_PM
diff --git a/drivers/base/power/shutdown.c b/drivers/base/power/shutdown.c
deleted file mode 100644
index 56e8eaaac012..000000000000
--- a/drivers/base/power/shutdown.c
+++ /dev/null
@@ -1,48 +0,0 @@
1/*
2 * shutdown.c - power management functions for the device tree.
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * 2002-3 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
8 *
9 */
10
11#include <linux/device.h>
12#include <asm/semaphore.h>
13
14#include "../base.h"
15#include "power.h"
16
17#define to_dev(node) container_of(node, struct device, kobj.entry)
18
19
20/**
21 * We handle system devices differently - we suspend and shut them
22 * down last and resume them first. That way, we don't do anything stupid like
23 * shutting down the interrupt controller before any devices..
24 *
25 * Note that there are not different stages for power management calls -
26 * they only get one called once when interrupts are disabled.
27 */
28
29
30/**
31 * device_shutdown - call ->shutdown() on each device to shutdown.
32 */
33void device_shutdown(void)
34{
35 struct device * dev, *devn;
36
37 list_for_each_entry_safe_reverse(dev, devn, &devices_subsys.list,
38 kobj.entry) {
39 if (dev->bus && dev->bus->shutdown) {
40 dev_dbg(dev, "shutdown\n");
41 dev->bus->shutdown(dev);
42 } else if (dev->driver && dev->driver->shutdown) {
43 dev_dbg(dev, "shutdown\n");
44 dev->driver->shutdown(dev);
45 }
46 }
47}
48
diff --git a/drivers/base/sys.c b/drivers/base/sys.c
index ac7ff6d0c6e5..2f79c55acdcc 100644
--- a/drivers/base/sys.c
+++ b/drivers/base/sys.c
@@ -25,8 +25,6 @@
25 25
26#include "base.h" 26#include "base.h"
27 27
28extern struct kset devices_subsys;
29
30#define to_sysdev(k) container_of(k, struct sys_device, kobj) 28#define to_sysdev(k) container_of(k, struct sys_device, kobj)
31#define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr) 29#define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr)
32 30
@@ -128,18 +126,17 @@ void sysdev_class_remove_file(struct sysdev_class *c,
128} 126}
129EXPORT_SYMBOL_GPL(sysdev_class_remove_file); 127EXPORT_SYMBOL_GPL(sysdev_class_remove_file);
130 128
131/* 129static struct kset *system_kset;
132 * declare system_subsys
133 */
134static decl_subsys(system, &ktype_sysdev_class, NULL);
135 130
136int sysdev_class_register(struct sysdev_class * cls) 131int sysdev_class_register(struct sysdev_class * cls)
137{ 132{
138 pr_debug("Registering sysdev class '%s'\n", 133 pr_debug("Registering sysdev class '%s'\n",
139 kobject_name(&cls->kset.kobj)); 134 kobject_name(&cls->kset.kobj));
140 INIT_LIST_HEAD(&cls->drivers); 135 INIT_LIST_HEAD(&cls->drivers);
141 cls->kset.kobj.parent = &system_subsys.kobj; 136 cls->kset.kobj.parent = &system_kset->kobj;
142 cls->kset.kobj.kset = &system_subsys; 137 cls->kset.kobj.ktype = &ktype_sysdev_class;
138 cls->kset.kobj.kset = system_kset;
139 kobject_set_name(&cls->kset.kobj, cls->name);
143 return kset_register(&cls->kset); 140 return kset_register(&cls->kset);
144} 141}
145 142
@@ -228,20 +225,15 @@ int sysdev_register(struct sys_device * sysdev)
228 if (!cls) 225 if (!cls)
229 return -EINVAL; 226 return -EINVAL;
230 227
228 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
229
231 /* Make sure the kset is set */ 230 /* Make sure the kset is set */
232 sysdev->kobj.kset = &cls->kset; 231 sysdev->kobj.kset = &cls->kset;
233 232
234 /* But make sure we point to the right type for sysfs translation */
235 sysdev->kobj.ktype = &ktype_sysdev;
236 error = kobject_set_name(&sysdev->kobj, "%s%d",
237 kobject_name(&cls->kset.kobj), sysdev->id);
238 if (error)
239 return error;
240
241 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
242
243 /* Register the object */ 233 /* Register the object */
244 error = kobject_register(&sysdev->kobj); 234 error = kobject_init_and_add(&sysdev->kobj, &ktype_sysdev, NULL,
235 "%s%d", kobject_name(&cls->kset.kobj),
236 sysdev->id);
245 237
246 if (!error) { 238 if (!error) {
247 struct sysdev_driver * drv; 239 struct sysdev_driver * drv;
@@ -258,6 +250,7 @@ int sysdev_register(struct sys_device * sysdev)
258 } 250 }
259 mutex_unlock(&sysdev_drivers_lock); 251 mutex_unlock(&sysdev_drivers_lock);
260 } 252 }
253 kobject_uevent(&sysdev->kobj, KOBJ_ADD);
261 return error; 254 return error;
262} 255}
263 256
@@ -272,7 +265,7 @@ void sysdev_unregister(struct sys_device * sysdev)
272 } 265 }
273 mutex_unlock(&sysdev_drivers_lock); 266 mutex_unlock(&sysdev_drivers_lock);
274 267
275 kobject_unregister(&sysdev->kobj); 268 kobject_put(&sysdev->kobj);
276} 269}
277 270
278 271
@@ -298,8 +291,7 @@ void sysdev_shutdown(void)
298 pr_debug("Shutting Down System Devices\n"); 291 pr_debug("Shutting Down System Devices\n");
299 292
300 mutex_lock(&sysdev_drivers_lock); 293 mutex_lock(&sysdev_drivers_lock);
301 list_for_each_entry_reverse(cls, &system_subsys.list, 294 list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
302 kset.kobj.entry) {
303 struct sys_device * sysdev; 295 struct sys_device * sysdev;
304 296
305 pr_debug("Shutting down type '%s':\n", 297 pr_debug("Shutting down type '%s':\n",
@@ -361,9 +353,7 @@ int sysdev_suspend(pm_message_t state)
361 353
362 pr_debug("Suspending System Devices\n"); 354 pr_debug("Suspending System Devices\n");
363 355
364 list_for_each_entry_reverse(cls, &system_subsys.list, 356 list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
365 kset.kobj.entry) {
366
367 pr_debug("Suspending type '%s':\n", 357 pr_debug("Suspending type '%s':\n",
368 kobject_name(&cls->kset.kobj)); 358 kobject_name(&cls->kset.kobj));
369 359
@@ -414,8 +404,7 @@ aux_driver:
414 } 404 }
415 405
416 /* resume other classes */ 406 /* resume other classes */
417 list_for_each_entry_continue(cls, &system_subsys.list, 407 list_for_each_entry_continue(cls, &system_kset->list, kset.kobj.entry) {
418 kset.kobj.entry) {
419 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) { 408 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
420 pr_debug(" %s\n", kobject_name(&err_dev->kobj)); 409 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
421 __sysdev_resume(err_dev); 410 __sysdev_resume(err_dev);
@@ -440,7 +429,7 @@ int sysdev_resume(void)
440 429
441 pr_debug("Resuming System Devices\n"); 430 pr_debug("Resuming System Devices\n");
442 431
443 list_for_each_entry(cls, &system_subsys.list, kset.kobj.entry) { 432 list_for_each_entry(cls, &system_kset->list, kset.kobj.entry) {
444 struct sys_device * sysdev; 433 struct sys_device * sysdev;
445 434
446 pr_debug("Resuming type '%s':\n", 435 pr_debug("Resuming type '%s':\n",
@@ -458,8 +447,10 @@ int sysdev_resume(void)
458 447
459int __init system_bus_init(void) 448int __init system_bus_init(void)
460{ 449{
461 system_subsys.kobj.parent = &devices_subsys.kobj; 450 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
462 return subsystem_register(&system_subsys); 451 if (!system_kset)
452 return -ENOMEM;
453 return 0;
463} 454}
464 455
465EXPORT_SYMBOL_GPL(sysdev_register); 456EXPORT_SYMBOL_GPL(sysdev_register);
diff --git a/drivers/block/aoe/aoeblk.c b/drivers/block/aoe/aoeblk.c
index ad00b3d94711..826d12381e21 100644
--- a/drivers/block/aoe/aoeblk.c
+++ b/drivers/block/aoe/aoeblk.c
@@ -15,8 +15,10 @@
15 15
16static struct kmem_cache *buf_pool_cache; 16static struct kmem_cache *buf_pool_cache;
17 17
18static ssize_t aoedisk_show_state(struct gendisk * disk, char *page) 18static ssize_t aoedisk_show_state(struct device *dev,
19 struct device_attribute *attr, char *page)
19{ 20{
21 struct gendisk *disk = dev_to_disk(dev);
20 struct aoedev *d = disk->private_data; 22 struct aoedev *d = disk->private_data;
21 23
22 return snprintf(page, PAGE_SIZE, 24 return snprintf(page, PAGE_SIZE,
@@ -26,50 +28,47 @@ static ssize_t aoedisk_show_state(struct gendisk * disk, char *page)
26 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : ""); 28 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
27 /* I'd rather see nopen exported so we can ditch closewait */ 29 /* I'd rather see nopen exported so we can ditch closewait */
28} 30}
29static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page) 31static ssize_t aoedisk_show_mac(struct device *dev,
32 struct device_attribute *attr, char *page)
30{ 33{
34 struct gendisk *disk = dev_to_disk(dev);
31 struct aoedev *d = disk->private_data; 35 struct aoedev *d = disk->private_data;
32 36
33 return snprintf(page, PAGE_SIZE, "%012llx\n", 37 return snprintf(page, PAGE_SIZE, "%012llx\n",
34 (unsigned long long)mac_addr(d->addr)); 38 (unsigned long long)mac_addr(d->addr));
35} 39}
36static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page) 40static ssize_t aoedisk_show_netif(struct device *dev,
41 struct device_attribute *attr, char *page)
37{ 42{
43 struct gendisk *disk = dev_to_disk(dev);
38 struct aoedev *d = disk->private_data; 44 struct aoedev *d = disk->private_data;
39 45
40 return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name); 46 return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
41} 47}
42/* firmware version */ 48/* firmware version */
43static ssize_t aoedisk_show_fwver(struct gendisk * disk, char *page) 49static ssize_t aoedisk_show_fwver(struct device *dev,
50 struct device_attribute *attr, char *page)
44{ 51{
52 struct gendisk *disk = dev_to_disk(dev);
45 struct aoedev *d = disk->private_data; 53 struct aoedev *d = disk->private_data;
46 54
47 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver); 55 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
48} 56}
49 57
50static struct disk_attribute disk_attr_state = { 58static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
51 .attr = {.name = "state", .mode = S_IRUGO }, 59static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
52 .show = aoedisk_show_state 60static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
53}; 61static struct device_attribute dev_attr_firmware_version = {
54static struct disk_attribute disk_attr_mac = { 62 .attr = { .name = "firmware-version", .mode = S_IRUGO, .owner = THIS_MODULE },
55 .attr = {.name = "mac", .mode = S_IRUGO }, 63 .show = aoedisk_show_fwver,
56 .show = aoedisk_show_mac
57};
58static struct disk_attribute disk_attr_netif = {
59 .attr = {.name = "netif", .mode = S_IRUGO },
60 .show = aoedisk_show_netif
61};
62static struct disk_attribute disk_attr_fwver = {
63 .attr = {.name = "firmware-version", .mode = S_IRUGO },
64 .show = aoedisk_show_fwver
65}; 64};
66 65
67static struct attribute *aoe_attrs[] = { 66static struct attribute *aoe_attrs[] = {
68 &disk_attr_state.attr, 67 &dev_attr_state.attr,
69 &disk_attr_mac.attr, 68 &dev_attr_mac.attr,
70 &disk_attr_netif.attr, 69 &dev_attr_netif.attr,
71 &disk_attr_fwver.attr, 70 &dev_attr_firmware_version.attr,
72 NULL 71 NULL,
73}; 72};
74 73
75static const struct attribute_group attr_group = { 74static const struct attribute_group attr_group = {
@@ -79,12 +78,12 @@ static const struct attribute_group attr_group = {
79static int 78static int
80aoedisk_add_sysfs(struct aoedev *d) 79aoedisk_add_sysfs(struct aoedev *d)
81{ 80{
82 return sysfs_create_group(&d->gd->kobj, &attr_group); 81 return sysfs_create_group(&d->gd->dev.kobj, &attr_group);
83} 82}
84void 83void
85aoedisk_rm_sysfs(struct aoedev *d) 84aoedisk_rm_sysfs(struct aoedev *d)
86{ 85{
87 sysfs_remove_group(&d->gd->kobj, &attr_group); 86 sysfs_remove_group(&d->gd->dev.kobj, &attr_group);
88} 87}
89 88
90static int 89static int
diff --git a/drivers/block/aoe/aoechr.c b/drivers/block/aoe/aoechr.c
index 39e563ea0878..d5480e34cb22 100644
--- a/drivers/block/aoe/aoechr.c
+++ b/drivers/block/aoe/aoechr.c
@@ -259,9 +259,8 @@ aoechr_init(void)
259 return PTR_ERR(aoe_class); 259 return PTR_ERR(aoe_class);
260 } 260 }
261 for (i = 0; i < ARRAY_SIZE(chardevs); ++i) 261 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
262 class_device_create(aoe_class, NULL, 262 device_create(aoe_class, NULL,
263 MKDEV(AOE_MAJOR, chardevs[i].minor), 263 MKDEV(AOE_MAJOR, chardevs[i].minor), chardevs[i].name);
264 NULL, chardevs[i].name);
265 264
266 return 0; 265 return 0;
267} 266}
@@ -272,7 +271,7 @@ aoechr_exit(void)
272 int i; 271 int i;
273 272
274 for (i = 0; i < ARRAY_SIZE(chardevs); ++i) 273 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
275 class_device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor)); 274 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
276 class_destroy(aoe_class); 275 class_destroy(aoe_class);
277 unregister_chrdev(AOE_MAJOR, "aoechr"); 276 unregister_chrdev(AOE_MAJOR, "aoechr");
278} 277}
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index b4c0888aedc3..ba9b17e507e0 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -375,14 +375,17 @@ harderror:
375 return NULL; 375 return NULL;
376} 376}
377 377
378static ssize_t pid_show(struct gendisk *disk, char *page) 378static ssize_t pid_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
379{ 380{
380 return sprintf(page, "%ld\n", 381 struct gendisk *disk = dev_to_disk(dev);
382
383 return sprintf(buf, "%ld\n",
381 (long) ((struct nbd_device *)disk->private_data)->pid); 384 (long) ((struct nbd_device *)disk->private_data)->pid);
382} 385}
383 386
384static struct disk_attribute pid_attr = { 387static struct device_attribute pid_attr = {
385 .attr = { .name = "pid", .mode = S_IRUGO }, 388 .attr = { .name = "pid", .mode = S_IRUGO, .owner = THIS_MODULE },
386 .show = pid_show, 389 .show = pid_show,
387}; 390};
388 391
@@ -394,7 +397,7 @@ static int nbd_do_it(struct nbd_device *lo)
394 BUG_ON(lo->magic != LO_MAGIC); 397 BUG_ON(lo->magic != LO_MAGIC);
395 398
396 lo->pid = current->pid; 399 lo->pid = current->pid;
397 ret = sysfs_create_file(&lo->disk->kobj, &pid_attr.attr); 400 ret = sysfs_create_file(&lo->disk->dev.kobj, &pid_attr.attr);
398 if (ret) { 401 if (ret) {
399 printk(KERN_ERR "nbd: sysfs_create_file failed!"); 402 printk(KERN_ERR "nbd: sysfs_create_file failed!");
400 return ret; 403 return ret;
@@ -403,7 +406,7 @@ static int nbd_do_it(struct nbd_device *lo)
403 while ((req = nbd_read_stat(lo)) != NULL) 406 while ((req = nbd_read_stat(lo)) != NULL)
404 nbd_end_request(req); 407 nbd_end_request(req);
405 408
406 sysfs_remove_file(&lo->disk->kobj, &pid_attr.attr); 409 sysfs_remove_file(&lo->disk->dev.kobj, &pid_attr.attr);
407 return 0; 410 return 0;
408} 411}
409 412
diff --git a/drivers/block/paride/pg.c b/drivers/block/paride/pg.c
index d89e7d32a3b6..ab86e23ddc69 100644
--- a/drivers/block/paride/pg.c
+++ b/drivers/block/paride/pg.c
@@ -676,8 +676,8 @@ static int __init pg_init(void)
676 for (unit = 0; unit < PG_UNITS; unit++) { 676 for (unit = 0; unit < PG_UNITS; unit++) {
677 struct pg *dev = &devices[unit]; 677 struct pg *dev = &devices[unit];
678 if (dev->present) 678 if (dev->present)
679 class_device_create(pg_class, NULL, MKDEV(major, unit), 679 device_create(pg_class, NULL, MKDEV(major, unit),
680 NULL, "pg%u", unit); 680 "pg%u", unit);
681 } 681 }
682 err = 0; 682 err = 0;
683 goto out; 683 goto out;
@@ -695,7 +695,7 @@ static void __exit pg_exit(void)
695 for (unit = 0; unit < PG_UNITS; unit++) { 695 for (unit = 0; unit < PG_UNITS; unit++) {
696 struct pg *dev = &devices[unit]; 696 struct pg *dev = &devices[unit];
697 if (dev->present) 697 if (dev->present)
698 class_device_destroy(pg_class, MKDEV(major, unit)); 698 device_destroy(pg_class, MKDEV(major, unit));
699 } 699 }
700 class_destroy(pg_class); 700 class_destroy(pg_class);
701 unregister_chrdev(major, name); 701 unregister_chrdev(major, name);
diff --git a/drivers/block/paride/pt.c b/drivers/block/paride/pt.c
index b91accf12656..76096cad798f 100644
--- a/drivers/block/paride/pt.c
+++ b/drivers/block/paride/pt.c
@@ -972,10 +972,10 @@ static int __init pt_init(void)
972 972
973 for (unit = 0; unit < PT_UNITS; unit++) 973 for (unit = 0; unit < PT_UNITS; unit++)
974 if (pt[unit].present) { 974 if (pt[unit].present) {
975 class_device_create(pt_class, NULL, MKDEV(major, unit), 975 device_create(pt_class, NULL, MKDEV(major, unit),
976 NULL, "pt%d", unit); 976 "pt%d", unit);
977 class_device_create(pt_class, NULL, MKDEV(major, unit + 128), 977 device_create(pt_class, NULL, MKDEV(major, unit + 128),
978 NULL, "pt%dn", unit); 978 "pt%dn", unit);
979 } 979 }
980 goto out; 980 goto out;
981 981
@@ -990,8 +990,8 @@ static void __exit pt_exit(void)
990 int unit; 990 int unit;
991 for (unit = 0; unit < PT_UNITS; unit++) 991 for (unit = 0; unit < PT_UNITS; unit++)
992 if (pt[unit].present) { 992 if (pt[unit].present) {
993 class_device_destroy(pt_class, MKDEV(major, unit)); 993 device_destroy(pt_class, MKDEV(major, unit));
994 class_device_destroy(pt_class, MKDEV(major, unit + 128)); 994 device_destroy(pt_class, MKDEV(major, unit + 128));
995 } 995 }
996 class_destroy(pt_class); 996 class_destroy(pt_class);
997 unregister_chrdev(major, name); 997 unregister_chrdev(major, name);
diff --git a/drivers/block/pktcdvd.c b/drivers/block/pktcdvd.c
index 3535ef896677..e9de1712e5a0 100644
--- a/drivers/block/pktcdvd.c
+++ b/drivers/block/pktcdvd.c
@@ -110,17 +110,18 @@ static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
110 struct kobj_type* ktype) 110 struct kobj_type* ktype)
111{ 111{
112 struct pktcdvd_kobj *p; 112 struct pktcdvd_kobj *p;
113 int error;
114
113 p = kzalloc(sizeof(*p), GFP_KERNEL); 115 p = kzalloc(sizeof(*p), GFP_KERNEL);
114 if (!p) 116 if (!p)
115 return NULL; 117 return NULL;
116 kobject_set_name(&p->kobj, "%s", name);
117 p->kobj.parent = parent;
118 p->kobj.ktype = ktype;
119 p->pd = pd; 118 p->pd = pd;
120 if (kobject_register(&p->kobj) != 0) { 119 error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
120 if (error) {
121 kobject_put(&p->kobj); 121 kobject_put(&p->kobj);
122 return NULL; 122 return NULL;
123 } 123 }
124 kobject_uevent(&p->kobj, KOBJ_ADD);
124 return p; 125 return p;
125} 126}
126/* 127/*
@@ -129,7 +130,7 @@ static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
129static void pkt_kobj_remove(struct pktcdvd_kobj *p) 130static void pkt_kobj_remove(struct pktcdvd_kobj *p)
130{ 131{
131 if (p) 132 if (p)
132 kobject_unregister(&p->kobj); 133 kobject_put(&p->kobj);
133} 134}
134/* 135/*
135 * default release function for pktcdvd kernel objects. 136 * default release function for pktcdvd kernel objects.
@@ -301,18 +302,16 @@ static struct kobj_type kobj_pkt_type_wqueue = {
301static void pkt_sysfs_dev_new(struct pktcdvd_device *pd) 302static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
302{ 303{
303 if (class_pktcdvd) { 304 if (class_pktcdvd) {
304 pd->clsdev = class_device_create(class_pktcdvd, 305 pd->dev = device_create(class_pktcdvd, NULL, pd->pkt_dev, "%s", pd->name);
305 NULL, pd->pkt_dev, 306 if (IS_ERR(pd->dev))
306 NULL, "%s", pd->name); 307 pd->dev = NULL;
307 if (IS_ERR(pd->clsdev))
308 pd->clsdev = NULL;
309 } 308 }
310 if (pd->clsdev) { 309 if (pd->dev) {
311 pd->kobj_stat = pkt_kobj_create(pd, "stat", 310 pd->kobj_stat = pkt_kobj_create(pd, "stat",
312 &pd->clsdev->kobj, 311 &pd->dev->kobj,
313 &kobj_pkt_type_stat); 312 &kobj_pkt_type_stat);
314 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue", 313 pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
315 &pd->clsdev->kobj, 314 &pd->dev->kobj,
316 &kobj_pkt_type_wqueue); 315 &kobj_pkt_type_wqueue);
317 } 316 }
318} 317}
@@ -322,7 +321,7 @@ static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
322 pkt_kobj_remove(pd->kobj_stat); 321 pkt_kobj_remove(pd->kobj_stat);
323 pkt_kobj_remove(pd->kobj_wqueue); 322 pkt_kobj_remove(pd->kobj_wqueue);
324 if (class_pktcdvd) 323 if (class_pktcdvd)
325 class_device_destroy(class_pktcdvd, pd->pkt_dev); 324 device_destroy(class_pktcdvd, pd->pkt_dev);
326} 325}
327 326
328 327
diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
index 2e3a0d4bc4c2..466629594776 100644
--- a/drivers/char/Kconfig
+++ b/drivers/char/Kconfig
@@ -373,6 +373,16 @@ config ISTALLION
373 To compile this driver as a module, choose M here: the 373 To compile this driver as a module, choose M here: the
374 module will be called istallion. 374 module will be called istallion.
375 375
376config NOZOMI
377 tristate "HSDPA Broadband Wireless Data Card - Globe Trotter"
378 depends on PCI && EXPERIMENTAL
379 help
380 If you have a HSDPA driver Broadband Wireless Data Card -
381 Globe Trotter PCMCIA card, say Y here.
382
383 To compile this driver as a module, choose M here, the module
384 will be called nozomi.
385
376config A2232 386config A2232
377 tristate "Commodore A2232 serial support (EXPERIMENTAL)" 387 tristate "Commodore A2232 serial support (EXPERIMENTAL)"
378 depends on EXPERIMENTAL && ZORRO && BROKEN_ON_SMP 388 depends on EXPERIMENTAL && ZORRO && BROKEN_ON_SMP
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index 07304d50e0cb..96fc01eddefe 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -26,6 +26,7 @@ obj-$(CONFIG_SERIAL167) += serial167.o
26obj-$(CONFIG_CYCLADES) += cyclades.o 26obj-$(CONFIG_CYCLADES) += cyclades.o
27obj-$(CONFIG_STALLION) += stallion.o 27obj-$(CONFIG_STALLION) += stallion.o
28obj-$(CONFIG_ISTALLION) += istallion.o 28obj-$(CONFIG_ISTALLION) += istallion.o
29obj-$(CONFIG_NOZOMI) += nozomi.o
29obj-$(CONFIG_DIGIEPCA) += epca.o 30obj-$(CONFIG_DIGIEPCA) += epca.o
30obj-$(CONFIG_SPECIALIX) += specialix.o 31obj-$(CONFIG_SPECIALIX) += specialix.o
31obj-$(CONFIG_MOXA_INTELLIO) += moxa.o 32obj-$(CONFIG_MOXA_INTELLIO) += moxa.o
diff --git a/drivers/char/hvc_console.c b/drivers/char/hvc_console.c
index 8252f8668538..480fae29c9b2 100644
--- a/drivers/char/hvc_console.c
+++ b/drivers/char/hvc_console.c
@@ -27,7 +27,7 @@
27#include <linux/init.h> 27#include <linux/init.h>
28#include <linux/kbd_kern.h> 28#include <linux/kbd_kern.h>
29#include <linux/kernel.h> 29#include <linux/kernel.h>
30#include <linux/kobject.h> 30#include <linux/kref.h>
31#include <linux/kthread.h> 31#include <linux/kthread.h>
32#include <linux/list.h> 32#include <linux/list.h>
33#include <linux/module.h> 33#include <linux/module.h>
@@ -89,7 +89,7 @@ struct hvc_struct {
89 int irq_requested; 89 int irq_requested;
90 int irq; 90 int irq;
91 struct list_head next; 91 struct list_head next;
92 struct kobject kobj; /* ref count & hvc_struct lifetime */ 92 struct kref kref; /* ref count & hvc_struct lifetime */
93}; 93};
94 94
95/* dynamic list of hvc_struct instances */ 95/* dynamic list of hvc_struct instances */
@@ -110,7 +110,7 @@ static int last_hvc = -1;
110 110
111/* 111/*
112 * Do not call this function with either the hvc_structs_lock or the hvc_struct 112 * Do not call this function with either the hvc_structs_lock or the hvc_struct
113 * lock held. If successful, this function increments the kobject reference 113 * lock held. If successful, this function increments the kref reference
114 * count against the target hvc_struct so it should be released when finished. 114 * count against the target hvc_struct so it should be released when finished.
115 */ 115 */
116static struct hvc_struct *hvc_get_by_index(int index) 116static struct hvc_struct *hvc_get_by_index(int index)
@@ -123,7 +123,7 @@ static struct hvc_struct *hvc_get_by_index(int index)
123 list_for_each_entry(hp, &hvc_structs, next) { 123 list_for_each_entry(hp, &hvc_structs, next) {
124 spin_lock_irqsave(&hp->lock, flags); 124 spin_lock_irqsave(&hp->lock, flags);
125 if (hp->index == index) { 125 if (hp->index == index) {
126 kobject_get(&hp->kobj); 126 kref_get(&hp->kref);
127 spin_unlock_irqrestore(&hp->lock, flags); 127 spin_unlock_irqrestore(&hp->lock, flags);
128 spin_unlock(&hvc_structs_lock); 128 spin_unlock(&hvc_structs_lock);
129 return hp; 129 return hp;
@@ -242,6 +242,23 @@ static int __init hvc_console_init(void)
242} 242}
243console_initcall(hvc_console_init); 243console_initcall(hvc_console_init);
244 244
245/* callback when the kboject ref count reaches zero. */
246static void destroy_hvc_struct(struct kref *kref)
247{
248 struct hvc_struct *hp = container_of(kref, struct hvc_struct, kref);
249 unsigned long flags;
250
251 spin_lock(&hvc_structs_lock);
252
253 spin_lock_irqsave(&hp->lock, flags);
254 list_del(&(hp->next));
255 spin_unlock_irqrestore(&hp->lock, flags);
256
257 spin_unlock(&hvc_structs_lock);
258
259 kfree(hp);
260}
261
245/* 262/*
246 * hvc_instantiate() is an early console discovery method which locates 263 * hvc_instantiate() is an early console discovery method which locates
247 * consoles * prior to the vio subsystem discovering them. Hotplugged 264 * consoles * prior to the vio subsystem discovering them. Hotplugged
@@ -261,7 +278,7 @@ int hvc_instantiate(uint32_t vtermno, int index, struct hv_ops *ops)
261 /* make sure no no tty has been registered in this index */ 278 /* make sure no no tty has been registered in this index */
262 hp = hvc_get_by_index(index); 279 hp = hvc_get_by_index(index);
263 if (hp) { 280 if (hp) {
264 kobject_put(&hp->kobj); 281 kref_put(&hp->kref, destroy_hvc_struct);
265 return -1; 282 return -1;
266 } 283 }
267 284
@@ -318,9 +335,8 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
318 unsigned long flags; 335 unsigned long flags;
319 int irq = 0; 336 int irq = 0;
320 int rc = 0; 337 int rc = 0;
321 struct kobject *kobjp;
322 338
323 /* Auto increments kobject reference if found. */ 339 /* Auto increments kref reference if found. */
324 if (!(hp = hvc_get_by_index(tty->index))) 340 if (!(hp = hvc_get_by_index(tty->index)))
325 return -ENODEV; 341 return -ENODEV;
326 342
@@ -341,8 +357,6 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
341 if (irq) 357 if (irq)
342 hp->irq_requested = 1; 358 hp->irq_requested = 1;
343 359
344 kobjp = &hp->kobj;
345
346 spin_unlock_irqrestore(&hp->lock, flags); 360 spin_unlock_irqrestore(&hp->lock, flags);
347 /* check error, fallback to non-irq */ 361 /* check error, fallback to non-irq */
348 if (irq) 362 if (irq)
@@ -352,7 +366,7 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
352 * If the request_irq() fails and we return an error. The tty layer 366 * If the request_irq() fails and we return an error. The tty layer
353 * will call hvc_close() after a failed open but we don't want to clean 367 * will call hvc_close() after a failed open but we don't want to clean
354 * up there so we'll clean up here and clear out the previously set 368 * up there so we'll clean up here and clear out the previously set
355 * tty fields and return the kobject reference. 369 * tty fields and return the kref reference.
356 */ 370 */
357 if (rc) { 371 if (rc) {
358 spin_lock_irqsave(&hp->lock, flags); 372 spin_lock_irqsave(&hp->lock, flags);
@@ -360,7 +374,7 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
360 hp->irq_requested = 0; 374 hp->irq_requested = 0;
361 spin_unlock_irqrestore(&hp->lock, flags); 375 spin_unlock_irqrestore(&hp->lock, flags);
362 tty->driver_data = NULL; 376 tty->driver_data = NULL;
363 kobject_put(kobjp); 377 kref_put(&hp->kref, destroy_hvc_struct);
364 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc); 378 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
365 } 379 }
366 /* Force wakeup of the polling thread */ 380 /* Force wakeup of the polling thread */
@@ -372,7 +386,6 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
372static void hvc_close(struct tty_struct *tty, struct file * filp) 386static void hvc_close(struct tty_struct *tty, struct file * filp)
373{ 387{
374 struct hvc_struct *hp; 388 struct hvc_struct *hp;
375 struct kobject *kobjp;
376 int irq = 0; 389 int irq = 0;
377 unsigned long flags; 390 unsigned long flags;
378 391
@@ -382,7 +395,7 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
382 /* 395 /*
383 * No driver_data means that this close was issued after a failed 396 * No driver_data means that this close was issued after a failed
384 * hvc_open by the tty layer's release_dev() function and we can just 397 * hvc_open by the tty layer's release_dev() function and we can just
385 * exit cleanly because the kobject reference wasn't made. 398 * exit cleanly because the kref reference wasn't made.
386 */ 399 */
387 if (!tty->driver_data) 400 if (!tty->driver_data)
388 return; 401 return;
@@ -390,7 +403,6 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
390 hp = tty->driver_data; 403 hp = tty->driver_data;
391 spin_lock_irqsave(&hp->lock, flags); 404 spin_lock_irqsave(&hp->lock, flags);
392 405
393 kobjp = &hp->kobj;
394 if (--hp->count == 0) { 406 if (--hp->count == 0) {
395 if (hp->irq_requested) 407 if (hp->irq_requested)
396 irq = hp->irq; 408 irq = hp->irq;
@@ -417,7 +429,7 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
417 spin_unlock_irqrestore(&hp->lock, flags); 429 spin_unlock_irqrestore(&hp->lock, flags);
418 } 430 }
419 431
420 kobject_put(kobjp); 432 kref_put(&hp->kref, destroy_hvc_struct);
421} 433}
422 434
423static void hvc_hangup(struct tty_struct *tty) 435static void hvc_hangup(struct tty_struct *tty)
@@ -426,7 +438,6 @@ static void hvc_hangup(struct tty_struct *tty)
426 unsigned long flags; 438 unsigned long flags;
427 int irq = 0; 439 int irq = 0;
428 int temp_open_count; 440 int temp_open_count;
429 struct kobject *kobjp;
430 441
431 if (!hp) 442 if (!hp)
432 return; 443 return;
@@ -443,7 +454,6 @@ static void hvc_hangup(struct tty_struct *tty)
443 return; 454 return;
444 } 455 }
445 456
446 kobjp = &hp->kobj;
447 temp_open_count = hp->count; 457 temp_open_count = hp->count;
448 hp->count = 0; 458 hp->count = 0;
449 hp->n_outbuf = 0; 459 hp->n_outbuf = 0;
@@ -457,7 +467,7 @@ static void hvc_hangup(struct tty_struct *tty)
457 free_irq(irq, hp); 467 free_irq(irq, hp);
458 while(temp_open_count) { 468 while(temp_open_count) {
459 --temp_open_count; 469 --temp_open_count;
460 kobject_put(kobjp); 470 kref_put(&hp->kref, destroy_hvc_struct);
461 } 471 }
462} 472}
463 473
@@ -729,27 +739,6 @@ static const struct tty_operations hvc_ops = {
729 .chars_in_buffer = hvc_chars_in_buffer, 739 .chars_in_buffer = hvc_chars_in_buffer,
730}; 740};
731 741
732/* callback when the kboject ref count reaches zero. */
733static void destroy_hvc_struct(struct kobject *kobj)
734{
735 struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
736 unsigned long flags;
737
738 spin_lock(&hvc_structs_lock);
739
740 spin_lock_irqsave(&hp->lock, flags);
741 list_del(&(hp->next));
742 spin_unlock_irqrestore(&hp->lock, flags);
743
744 spin_unlock(&hvc_structs_lock);
745
746 kfree(hp);
747}
748
749static struct kobj_type hvc_kobj_type = {
750 .release = destroy_hvc_struct,
751};
752
753struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq, 742struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
754 struct hv_ops *ops, int outbuf_size) 743 struct hv_ops *ops, int outbuf_size)
755{ 744{
@@ -776,8 +765,7 @@ struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
776 hp->outbuf_size = outbuf_size; 765 hp->outbuf_size = outbuf_size;
777 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))]; 766 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
778 767
779 kobject_init(&hp->kobj); 768 kref_init(&hp->kref);
780 hp->kobj.ktype = &hvc_kobj_type;
781 769
782 spin_lock_init(&hp->lock); 770 spin_lock_init(&hp->lock);
783 spin_lock(&hvc_structs_lock); 771 spin_lock(&hvc_structs_lock);
@@ -806,12 +794,10 @@ struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
806int __devexit hvc_remove(struct hvc_struct *hp) 794int __devexit hvc_remove(struct hvc_struct *hp)
807{ 795{
808 unsigned long flags; 796 unsigned long flags;
809 struct kobject *kobjp;
810 struct tty_struct *tty; 797 struct tty_struct *tty;
811 798
812 spin_lock_irqsave(&hp->lock, flags); 799 spin_lock_irqsave(&hp->lock, flags);
813 tty = hp->tty; 800 tty = hp->tty;
814 kobjp = &hp->kobj;
815 801
816 if (hp->index < MAX_NR_HVC_CONSOLES) 802 if (hp->index < MAX_NR_HVC_CONSOLES)
817 vtermnos[hp->index] = -1; 803 vtermnos[hp->index] = -1;
@@ -821,12 +807,12 @@ int __devexit hvc_remove(struct hvc_struct *hp)
821 spin_unlock_irqrestore(&hp->lock, flags); 807 spin_unlock_irqrestore(&hp->lock, flags);
822 808
823 /* 809 /*
824 * We 'put' the instance that was grabbed when the kobject instance 810 * We 'put' the instance that was grabbed when the kref instance
825 * was initialized using kobject_init(). Let the last holder of this 811 * was initialized using kref_init(). Let the last holder of this
826 * kobject cause it to be removed, which will probably be the tty_hangup 812 * kref cause it to be removed, which will probably be the tty_hangup
827 * below. 813 * below.
828 */ 814 */
829 kobject_put(kobjp); 815 kref_put(&hp->kref, destroy_hvc_struct);
830 816
831 /* 817 /*
832 * This function call will auto chain call hvc_hangup. The tty should 818 * This function call will auto chain call hvc_hangup. The tty should
diff --git a/drivers/char/hvcs.c b/drivers/char/hvcs.c
index 69d8866de783..fd7559084b82 100644
--- a/drivers/char/hvcs.c
+++ b/drivers/char/hvcs.c
@@ -57,11 +57,7 @@
57 * rescanning partner information upon a user's request. 57 * rescanning partner information upon a user's request.
58 * 58 *
59 * Each vty-server, prior to being exposed to this driver is reference counted 59 * Each vty-server, prior to being exposed to this driver is reference counted
60 * using the 2.6 Linux kernel kobject construct. This kobject is also used by 60 * using the 2.6 Linux kernel kref construct.
61 * the vio bus to provide a vio device sysfs entry that this driver attaches
62 * device specific attributes to, including partner information. The vio bus
63 * framework also provides a sysfs entry for each vio driver. The hvcs driver
64 * provides driver attributes in this entry.
65 * 61 *
66 * For direction on installation and usage of this driver please reference 62 * For direction on installation and usage of this driver please reference
67 * Documentation/powerpc/hvcs.txt. 63 * Documentation/powerpc/hvcs.txt.
@@ -71,7 +67,7 @@
71#include <linux/init.h> 67#include <linux/init.h>
72#include <linux/interrupt.h> 68#include <linux/interrupt.h>
73#include <linux/kernel.h> 69#include <linux/kernel.h>
74#include <linux/kobject.h> 70#include <linux/kref.h>
75#include <linux/kthread.h> 71#include <linux/kthread.h>
76#include <linux/list.h> 72#include <linux/list.h>
77#include <linux/major.h> 73#include <linux/major.h>
@@ -293,12 +289,12 @@ struct hvcs_struct {
293 int chars_in_buffer; 289 int chars_in_buffer;
294 290
295 /* 291 /*
296 * Any variable below the kobject is valid before a tty is connected and 292 * Any variable below the kref is valid before a tty is connected and
297 * stays valid after the tty is disconnected. These shouldn't be 293 * stays valid after the tty is disconnected. These shouldn't be
298 * whacked until the koject refcount reaches zero though some entries 294 * whacked until the koject refcount reaches zero though some entries
299 * may be changed via sysfs initiatives. 295 * may be changed via sysfs initiatives.
300 */ 296 */
301 struct kobject kobj; /* ref count & hvcs_struct lifetime */ 297 struct kref kref; /* ref count & hvcs_struct lifetime */
302 int connected; /* is the vty-server currently connected to a vty? */ 298 int connected; /* is the vty-server currently connected to a vty? */
303 uint32_t p_unit_address; /* partner unit address */ 299 uint32_t p_unit_address; /* partner unit address */
304 uint32_t p_partition_ID; /* partner partition ID */ 300 uint32_t p_partition_ID; /* partner partition ID */
@@ -307,8 +303,8 @@ struct hvcs_struct {
307 struct vio_dev *vdev; 303 struct vio_dev *vdev;
308}; 304};
309 305
310/* Required to back map a kobject to its containing object */ 306/* Required to back map a kref to its containing object */
311#define from_kobj(kobj) container_of(kobj, struct hvcs_struct, kobj) 307#define from_kref(k) container_of(k, struct hvcs_struct, kref)
312 308
313static struct list_head hvcs_structs = LIST_HEAD_INIT(hvcs_structs); 309static struct list_head hvcs_structs = LIST_HEAD_INIT(hvcs_structs);
314static DEFINE_SPINLOCK(hvcs_structs_lock); 310static DEFINE_SPINLOCK(hvcs_structs_lock);
@@ -334,7 +330,6 @@ static void hvcs_partner_free(struct hvcs_struct *hvcsd);
334static int hvcs_enable_device(struct hvcs_struct *hvcsd, 330static int hvcs_enable_device(struct hvcs_struct *hvcsd,
335 uint32_t unit_address, unsigned int irq, struct vio_dev *dev); 331 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
336 332
337static void destroy_hvcs_struct(struct kobject *kobj);
338static int hvcs_open(struct tty_struct *tty, struct file *filp); 333static int hvcs_open(struct tty_struct *tty, struct file *filp);
339static void hvcs_close(struct tty_struct *tty, struct file *filp); 334static void hvcs_close(struct tty_struct *tty, struct file *filp);
340static void hvcs_hangup(struct tty_struct * tty); 335static void hvcs_hangup(struct tty_struct * tty);
@@ -703,10 +698,10 @@ static void hvcs_return_index(int index)
703 hvcs_index_list[index] = -1; 698 hvcs_index_list[index] = -1;
704} 699}
705 700
706/* callback when the kboject ref count reaches zero */ 701/* callback when the kref ref count reaches zero */
707static void destroy_hvcs_struct(struct kobject *kobj) 702static void destroy_hvcs_struct(struct kref *kref)
708{ 703{
709 struct hvcs_struct *hvcsd = from_kobj(kobj); 704 struct hvcs_struct *hvcsd = from_kref(kref);
710 struct vio_dev *vdev; 705 struct vio_dev *vdev;
711 unsigned long flags; 706 unsigned long flags;
712 707
@@ -743,10 +738,6 @@ static void destroy_hvcs_struct(struct kobject *kobj)
743 kfree(hvcsd); 738 kfree(hvcsd);
744} 739}
745 740
746static struct kobj_type hvcs_kobj_type = {
747 .release = destroy_hvcs_struct,
748};
749
750static int hvcs_get_index(void) 741static int hvcs_get_index(void)
751{ 742{
752 int i; 743 int i;
@@ -791,9 +782,7 @@ static int __devinit hvcs_probe(
791 782
792 spin_lock_init(&hvcsd->lock); 783 spin_lock_init(&hvcsd->lock);
793 /* Automatically incs the refcount the first time */ 784 /* Automatically incs the refcount the first time */
794 kobject_init(&hvcsd->kobj); 785 kref_init(&hvcsd->kref);
795 /* Set up the callback for terminating the hvcs_struct's life */
796 hvcsd->kobj.ktype = &hvcs_kobj_type;
797 786
798 hvcsd->vdev = dev; 787 hvcsd->vdev = dev;
799 dev->dev.driver_data = hvcsd; 788 dev->dev.driver_data = hvcsd;
@@ -844,7 +833,6 @@ static int __devexit hvcs_remove(struct vio_dev *dev)
844{ 833{
845 struct hvcs_struct *hvcsd = dev->dev.driver_data; 834 struct hvcs_struct *hvcsd = dev->dev.driver_data;
846 unsigned long flags; 835 unsigned long flags;
847 struct kobject *kobjp;
848 struct tty_struct *tty; 836 struct tty_struct *tty;
849 837
850 if (!hvcsd) 838 if (!hvcsd)
@@ -856,15 +844,13 @@ static int __devexit hvcs_remove(struct vio_dev *dev)
856 844
857 tty = hvcsd->tty; 845 tty = hvcsd->tty;
858 846
859 kobjp = &hvcsd->kobj;
860
861 spin_unlock_irqrestore(&hvcsd->lock, flags); 847 spin_unlock_irqrestore(&hvcsd->lock, flags);
862 848
863 /* 849 /*
864 * Let the last holder of this object cause it to be removed, which 850 * Let the last holder of this object cause it to be removed, which
865 * would probably be tty_hangup below. 851 * would probably be tty_hangup below.
866 */ 852 */
867 kobject_put (kobjp); 853 kref_put(&hvcsd->kref, destroy_hvcs_struct);
868 854
869 /* 855 /*
870 * The hangup is a scheduled function which will auto chain call 856 * The hangup is a scheduled function which will auto chain call
@@ -1086,7 +1072,7 @@ static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1086} 1072}
1087 1073
1088/* 1074/*
1089 * This always increments the kobject ref count if the call is successful. 1075 * This always increments the kref ref count if the call is successful.
1090 * Please remember to dec when you are done with the instance. 1076 * Please remember to dec when you are done with the instance.
1091 * 1077 *
1092 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when 1078 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
@@ -1103,7 +1089,7 @@ static struct hvcs_struct *hvcs_get_by_index(int index)
1103 list_for_each_entry(hvcsd, &hvcs_structs, next) { 1089 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1104 spin_lock_irqsave(&hvcsd->lock, flags); 1090 spin_lock_irqsave(&hvcsd->lock, flags);
1105 if (hvcsd->index == index) { 1091 if (hvcsd->index == index) {
1106 kobject_get(&hvcsd->kobj); 1092 kref_get(&hvcsd->kref);
1107 spin_unlock_irqrestore(&hvcsd->lock, flags); 1093 spin_unlock_irqrestore(&hvcsd->lock, flags);
1108 spin_unlock(&hvcs_structs_lock); 1094 spin_unlock(&hvcs_structs_lock);
1109 return hvcsd; 1095 return hvcsd;
@@ -1129,14 +1115,13 @@ static int hvcs_open(struct tty_struct *tty, struct file *filp)
1129 unsigned int irq; 1115 unsigned int irq;
1130 struct vio_dev *vdev; 1116 struct vio_dev *vdev;
1131 unsigned long unit_address; 1117 unsigned long unit_address;
1132 struct kobject *kobjp;
1133 1118
1134 if (tty->driver_data) 1119 if (tty->driver_data)
1135 goto fast_open; 1120 goto fast_open;
1136 1121
1137 /* 1122 /*
1138 * Is there a vty-server that shares the same index? 1123 * Is there a vty-server that shares the same index?
1139 * This function increments the kobject index. 1124 * This function increments the kref index.
1140 */ 1125 */
1141 if (!(hvcsd = hvcs_get_by_index(tty->index))) { 1126 if (!(hvcsd = hvcs_get_by_index(tty->index))) {
1142 printk(KERN_WARNING "HVCS: open failed, no device associated" 1127 printk(KERN_WARNING "HVCS: open failed, no device associated"
@@ -1181,7 +1166,7 @@ static int hvcs_open(struct tty_struct *tty, struct file *filp)
1181 * and will grab the spinlock and free the connection if it fails. 1166 * and will grab the spinlock and free the connection if it fails.
1182 */ 1167 */
1183 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) { 1168 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
1184 kobject_put(&hvcsd->kobj); 1169 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1185 printk(KERN_WARNING "HVCS: enable device failed.\n"); 1170 printk(KERN_WARNING "HVCS: enable device failed.\n");
1186 return rc; 1171 return rc;
1187 } 1172 }
@@ -1192,17 +1177,11 @@ fast_open:
1192 hvcsd = tty->driver_data; 1177 hvcsd = tty->driver_data;
1193 1178
1194 spin_lock_irqsave(&hvcsd->lock, flags); 1179 spin_lock_irqsave(&hvcsd->lock, flags);
1195 if (!kobject_get(&hvcsd->kobj)) { 1180 kref_get(&hvcsd->kref);
1196 spin_unlock_irqrestore(&hvcsd->lock, flags);
1197 printk(KERN_ERR "HVCS: Kobject of open"
1198 " hvcs doesn't exist.\n");
1199 return -EFAULT; /* Is this the right return value? */
1200 }
1201
1202 hvcsd->open_count++; 1181 hvcsd->open_count++;
1203
1204 hvcsd->todo_mask |= HVCS_SCHED_READ; 1182 hvcsd->todo_mask |= HVCS_SCHED_READ;
1205 spin_unlock_irqrestore(&hvcsd->lock, flags); 1183 spin_unlock_irqrestore(&hvcsd->lock, flags);
1184
1206open_success: 1185open_success:
1207 hvcs_kick(); 1186 hvcs_kick();
1208 1187
@@ -1212,9 +1191,8 @@ open_success:
1212 return 0; 1191 return 0;
1213 1192
1214error_release: 1193error_release:
1215 kobjp = &hvcsd->kobj;
1216 spin_unlock_irqrestore(&hvcsd->lock, flags); 1194 spin_unlock_irqrestore(&hvcsd->lock, flags);
1217 kobject_put(&hvcsd->kobj); 1195 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1218 1196
1219 printk(KERN_WARNING "HVCS: partner connect failed.\n"); 1197 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1220 return retval; 1198 return retval;
@@ -1224,7 +1202,6 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
1224{ 1202{
1225 struct hvcs_struct *hvcsd; 1203 struct hvcs_struct *hvcsd;
1226 unsigned long flags; 1204 unsigned long flags;
1227 struct kobject *kobjp;
1228 int irq = NO_IRQ; 1205 int irq = NO_IRQ;
1229 1206
1230 /* 1207 /*
@@ -1245,7 +1222,6 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
1245 hvcsd = tty->driver_data; 1222 hvcsd = tty->driver_data;
1246 1223
1247 spin_lock_irqsave(&hvcsd->lock, flags); 1224 spin_lock_irqsave(&hvcsd->lock, flags);
1248 kobjp = &hvcsd->kobj;
1249 if (--hvcsd->open_count == 0) { 1225 if (--hvcsd->open_count == 0) {
1250 1226
1251 vio_disable_interrupts(hvcsd->vdev); 1227 vio_disable_interrupts(hvcsd->vdev);
@@ -1270,7 +1246,7 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
1270 tty->driver_data = NULL; 1246 tty->driver_data = NULL;
1271 1247
1272 free_irq(irq, hvcsd); 1248 free_irq(irq, hvcsd);
1273 kobject_put(kobjp); 1249 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1274 return; 1250 return;
1275 } else if (hvcsd->open_count < 0) { 1251 } else if (hvcsd->open_count < 0) {
1276 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d" 1252 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
@@ -1279,7 +1255,7 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
1279 } 1255 }
1280 1256
1281 spin_unlock_irqrestore(&hvcsd->lock, flags); 1257 spin_unlock_irqrestore(&hvcsd->lock, flags);
1282 kobject_put(kobjp); 1258 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1283} 1259}
1284 1260
1285static void hvcs_hangup(struct tty_struct * tty) 1261static void hvcs_hangup(struct tty_struct * tty)
@@ -1287,21 +1263,17 @@ static void hvcs_hangup(struct tty_struct * tty)
1287 struct hvcs_struct *hvcsd = tty->driver_data; 1263 struct hvcs_struct *hvcsd = tty->driver_data;
1288 unsigned long flags; 1264 unsigned long flags;
1289 int temp_open_count; 1265 int temp_open_count;
1290 struct kobject *kobjp;
1291 int irq = NO_IRQ; 1266 int irq = NO_IRQ;
1292 1267
1293 spin_lock_irqsave(&hvcsd->lock, flags); 1268 spin_lock_irqsave(&hvcsd->lock, flags);
1294 /* Preserve this so that we know how many kobject refs to put */ 1269 /* Preserve this so that we know how many kref refs to put */
1295 temp_open_count = hvcsd->open_count; 1270 temp_open_count = hvcsd->open_count;
1296 1271
1297 /* 1272 /*
1298 * Don't kobject put inside the spinlock because the destruction 1273 * Don't kref put inside the spinlock because the destruction
1299 * callback may use the spinlock and it may get called before the 1274 * callback may use the spinlock and it may get called before the
1300 * spinlock has been released. Get a pointer to the kobject and 1275 * spinlock has been released.
1301 * kobject_put on that after releasing the spinlock.
1302 */ 1276 */
1303 kobjp = &hvcsd->kobj;
1304
1305 vio_disable_interrupts(hvcsd->vdev); 1277 vio_disable_interrupts(hvcsd->vdev);
1306 1278
1307 hvcsd->todo_mask = 0; 1279 hvcsd->todo_mask = 0;
@@ -1324,7 +1296,7 @@ static void hvcs_hangup(struct tty_struct * tty)
1324 free_irq(irq, hvcsd); 1296 free_irq(irq, hvcsd);
1325 1297
1326 /* 1298 /*
1327 * We need to kobject_put() for every open_count we have since the 1299 * We need to kref_put() for every open_count we have since the
1328 * tty_hangup() function doesn't invoke a close per open connection on a 1300 * tty_hangup() function doesn't invoke a close per open connection on a
1329 * non-console device. 1301 * non-console device.
1330 */ 1302 */
@@ -1335,7 +1307,7 @@ static void hvcs_hangup(struct tty_struct * tty)
1335 * NOTE: If this hangup was signaled from user space then the 1307 * NOTE: If this hangup was signaled from user space then the
1336 * final put will never happen. 1308 * final put will never happen.
1337 */ 1309 */
1338 kobject_put(kobjp); 1310 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1339 } 1311 }
1340} 1312}
1341 1313
diff --git a/drivers/char/nozomi.c b/drivers/char/nozomi.c
new file mode 100644
index 000000000000..6076e662886a
--- /dev/null
+++ b/drivers/char/nozomi.c
@@ -0,0 +1,1993 @@
1/*
2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3 *
4 * Written by: Ulf Jakobsson,
5 * Jan �erfeldt,
6 * Stefan Thomasson,
7 *
8 * Maintained by: Paul Hardwick (p.hardwick@option.com)
9 *
10 * Patches:
11 * Locking code changes for Vodafone by Sphere Systems Ltd,
12 * Andrew Bird (ajb@spheresystems.co.uk )
13 * & Phil Sanderson
14 *
15 * Source has been ported from an implementation made by Filip Aben @ Option
16 *
17 * --------------------------------------------------------------------------
18 *
19 * Copyright (c) 2005,2006 Option Wireless Sweden AB
20 * Copyright (c) 2006 Sphere Systems Ltd
21 * Copyright (c) 2006 Option Wireless n/v
22 * All rights Reserved.
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 *
38 * --------------------------------------------------------------------------
39 */
40
41/*
42 * CHANGELOG
43 * Version 2.1d
44 * 11-November-2007 Jiri Slaby, Frank Seidel
45 * - Big rework of multicard support by Jiri
46 * - Major cleanups (semaphore to mutex, endianess, no major reservation)
47 * - Optimizations
48 *
49 * Version 2.1c
50 * 30-October-2007 Frank Seidel
51 * - Completed multicard support
52 * - Minor cleanups
53 *
54 * Version 2.1b
55 * 07-August-2007 Frank Seidel
56 * - Minor cleanups
57 * - theoretical multicard support
58 *
59 * Version 2.1
60 * 03-July-2006 Paul Hardwick
61 *
62 * - Stability Improvements. Incorporated spinlock wraps patch.
63 * - Updated for newer 2.6.14+ kernels (tty_buffer_request_room)
64 * - using __devexit macro for tty
65 *
66 *
67 * Version 2.0
68 * 08-feb-2006 15:34:10:Ulf
69 *
70 * -Fixed issue when not waking up line disipine layer, could probably result
71 * in better uplink performance for 2.4.
72 *
73 * -Fixed issue with big endian during initalization, now proper toggle flags
74 * are handled between preloader and maincode.
75 *
76 * -Fixed flow control issue.
77 *
78 * -Added support for setting DTR.
79 *
80 * -For 2.4 kernels, removing temporary buffer that's not needed.
81 *
82 * -Reading CTS only for modem port (only port that supports it).
83 *
84 * -Return 0 in write_room instead of netative value, it's not handled in
85 * upper layer.
86 *
87 * --------------------------------------------------------------------------
88 * Version 1.0
89 *
90 * First version of driver, only tested with card of type F32_2.
91 * Works fine with 2.4 and 2.6 kernels.
92 * Driver also support big endian architecture.
93 */
94
95/* Enable this to have a lot of debug printouts */
96#define DEBUG
97
98#include <linux/kernel.h>
99#include <linux/module.h>
100#include <linux/pci.h>
101#include <linux/ioport.h>
102#include <linux/tty.h>
103#include <linux/tty_driver.h>
104#include <linux/tty_flip.h>
105#include <linux/serial.h>
106#include <linux/interrupt.h>
107#include <linux/kmod.h>
108#include <linux/init.h>
109#include <linux/kfifo.h>
110#include <linux/uaccess.h>
111#include <asm/byteorder.h>
112
113#include <linux/delay.h>
114
115
116#define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
117 __DATE__ " " __TIME__ ")"
118
119/* Macros definitions */
120
121/* Default debug printout level */
122#define NOZOMI_DEBUG_LEVEL 0x00
123
124#define P_BUF_SIZE 128
125#define NFO(_err_flag_, args...) \
126do { \
127 char tmp[P_BUF_SIZE]; \
128 snprintf(tmp, sizeof(tmp), ##args); \
129 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
130 __FUNCTION__, tmp); \
131} while (0)
132
133#define DBG1(args...) D_(0x01, ##args)
134#define DBG2(args...) D_(0x02, ##args)
135#define DBG3(args...) D_(0x04, ##args)
136#define DBG4(args...) D_(0x08, ##args)
137#define DBG5(args...) D_(0x10, ##args)
138#define DBG6(args...) D_(0x20, ##args)
139#define DBG7(args...) D_(0x40, ##args)
140#define DBG8(args...) D_(0x80, ##args)
141
142#ifdef DEBUG
143/* Do we need this settable at runtime? */
144static int debug = NOZOMI_DEBUG_LEVEL;
145
146#define D(lvl, args...) do {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
147 while (0)
148#define D_(lvl, args...) D(lvl, ##args)
149
150/* These printouts are always printed */
151
152#else
153static int debug;
154#define D_(lvl, args...)
155#endif
156
157/* TODO: rewrite to optimize macros... */
158
159#define TMP_BUF_MAX 256
160
161#define DUMP(buf__,len__) \
162 do { \
163 char tbuf[TMP_BUF_MAX] = {0};\
164 if (len__ > 1) {\
165 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
166 if (tbuf[len__-2] == '\r') {\
167 tbuf[len__-2] = 'r';\
168 } \
169 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
170 } else {\
171 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
172 } \
173} while (0)
174
175/* Defines */
176#define NOZOMI_NAME "nozomi"
177#define NOZOMI_NAME_TTY "nozomi_tty"
178#define DRIVER_DESC "Nozomi driver"
179
180#define NTTY_TTY_MAXMINORS 256
181#define NTTY_FIFO_BUFFER_SIZE 8192
182
183/* Must be power of 2 */
184#define FIFO_BUFFER_SIZE_UL 8192
185
186/* Size of tmp send buffer to card */
187#define SEND_BUF_MAX 1024
188#define RECEIVE_BUF_MAX 4
189
190
191/* Define all types of vendors and devices to support */
192#define VENDOR1 0x1931 /* Vendor Option */
193#define DEVICE1 0x000c /* HSDPA card */
194
195#define R_IIR 0x0000 /* Interrupt Identity Register */
196#define R_FCR 0x0000 /* Flow Control Register */
197#define R_IER 0x0004 /* Interrupt Enable Register */
198
199#define CONFIG_MAGIC 0xEFEFFEFE
200#define TOGGLE_VALID 0x0000
201
202/* Definition of interrupt tokens */
203#define MDM_DL1 0x0001
204#define MDM_UL1 0x0002
205#define MDM_DL2 0x0004
206#define MDM_UL2 0x0008
207#define DIAG_DL1 0x0010
208#define DIAG_DL2 0x0020
209#define DIAG_UL 0x0040
210#define APP1_DL 0x0080
211#define APP1_UL 0x0100
212#define APP2_DL 0x0200
213#define APP2_UL 0x0400
214#define CTRL_DL 0x0800
215#define CTRL_UL 0x1000
216#define RESET 0x8000
217
218#define MDM_DL (MDM_DL1 | MDM_DL2)
219#define MDM_UL (MDM_UL1 | MDM_UL2)
220#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
221
222/* modem signal definition */
223#define CTRL_DSR 0x0001
224#define CTRL_DCD 0x0002
225#define CTRL_RI 0x0004
226#define CTRL_CTS 0x0008
227
228#define CTRL_DTR 0x0001
229#define CTRL_RTS 0x0002
230
231#define MAX_PORT 4
232#define NOZOMI_MAX_PORTS 5
233#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
234
235/* Type definitions */
236
237/*
238 * There are two types of nozomi cards,
239 * one with 2048 memory and with 8192 memory
240 */
241enum card_type {
242 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
243 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
244};
245
246/* Two different toggle channels exist */
247enum channel_type {
248 CH_A = 0,
249 CH_B = 1,
250};
251
252/* Port definition for the card regarding flow control */
253enum ctrl_port_type {
254 CTRL_CMD = 0,
255 CTRL_MDM = 1,
256 CTRL_DIAG = 2,
257 CTRL_APP1 = 3,
258 CTRL_APP2 = 4,
259 CTRL_ERROR = -1,
260};
261
262/* Ports that the nozomi has */
263enum port_type {
264 PORT_MDM = 0,
265 PORT_DIAG = 1,
266 PORT_APP1 = 2,
267 PORT_APP2 = 3,
268 PORT_CTRL = 4,
269 PORT_ERROR = -1,
270};
271
272#ifdef __BIG_ENDIAN
273/* Big endian */
274
275struct toggles {
276 unsigned enabled:5; /*
277 * Toggle fields are valid if enabled is 0,
278 * else A-channels must always be used.
279 */
280 unsigned diag_dl:1;
281 unsigned mdm_dl:1;
282 unsigned mdm_ul:1;
283} __attribute__ ((packed));
284
285/* Configuration table to read at startup of card */
286/* Is for now only needed during initialization phase */
287struct config_table {
288 u32 signature;
289 u16 product_information;
290 u16 version;
291 u8 pad3[3];
292 struct toggles toggle;
293 u8 pad1[4];
294 u16 dl_mdm_len1; /*
295 * If this is 64, it can hold
296 * 60 bytes + 4 that is length field
297 */
298 u16 dl_start;
299
300 u16 dl_diag_len1;
301 u16 dl_mdm_len2; /*
302 * If this is 64, it can hold
303 * 60 bytes + 4 that is length field
304 */
305 u16 dl_app1_len;
306
307 u16 dl_diag_len2;
308 u16 dl_ctrl_len;
309 u16 dl_app2_len;
310 u8 pad2[16];
311 u16 ul_mdm_len1;
312 u16 ul_start;
313 u16 ul_diag_len;
314 u16 ul_mdm_len2;
315 u16 ul_app1_len;
316 u16 ul_app2_len;
317 u16 ul_ctrl_len;
318} __attribute__ ((packed));
319
320/* This stores all control downlink flags */
321struct ctrl_dl {
322 u8 port;
323 unsigned reserved:4;
324 unsigned CTS:1;
325 unsigned RI:1;
326 unsigned DCD:1;
327 unsigned DSR:1;
328} __attribute__ ((packed));
329
330/* This stores all control uplink flags */
331struct ctrl_ul {
332 u8 port;
333 unsigned reserved:6;
334 unsigned RTS:1;
335 unsigned DTR:1;
336} __attribute__ ((packed));
337
338#else
339/* Little endian */
340
341/* This represents the toggle information */
342struct toggles {
343 unsigned mdm_ul:1;
344 unsigned mdm_dl:1;
345 unsigned diag_dl:1;
346 unsigned enabled:5; /*
347 * Toggle fields are valid if enabled is 0,
348 * else A-channels must always be used.
349 */
350} __attribute__ ((packed));
351
352/* Configuration table to read at startup of card */
353struct config_table {
354 u32 signature;
355 u16 version;
356 u16 product_information;
357 struct toggles toggle;
358 u8 pad1[7];
359 u16 dl_start;
360 u16 dl_mdm_len1; /*
361 * If this is 64, it can hold
362 * 60 bytes + 4 that is length field
363 */
364 u16 dl_mdm_len2;
365 u16 dl_diag_len1;
366 u16 dl_diag_len2;
367 u16 dl_app1_len;
368 u16 dl_app2_len;
369 u16 dl_ctrl_len;
370 u8 pad2[16];
371 u16 ul_start;
372 u16 ul_mdm_len2;
373 u16 ul_mdm_len1;
374 u16 ul_diag_len;
375 u16 ul_app1_len;
376 u16 ul_app2_len;
377 u16 ul_ctrl_len;
378} __attribute__ ((packed));
379
380/* This stores all control downlink flags */
381struct ctrl_dl {
382 unsigned DSR:1;
383 unsigned DCD:1;
384 unsigned RI:1;
385 unsigned CTS:1;
386 unsigned reserverd:4;
387 u8 port;
388} __attribute__ ((packed));
389
390/* This stores all control uplink flags */
391struct ctrl_ul {
392 unsigned DTR:1;
393 unsigned RTS:1;
394 unsigned reserved:6;
395 u8 port;
396} __attribute__ ((packed));
397#endif
398
399/* This holds all information that is needed regarding a port */
400struct port {
401 u8 update_flow_control;
402 struct ctrl_ul ctrl_ul;
403 struct ctrl_dl ctrl_dl;
404 struct kfifo *fifo_ul;
405 void __iomem *dl_addr[2];
406 u32 dl_size[2];
407 u8 toggle_dl;
408 void __iomem *ul_addr[2];
409 u32 ul_size[2];
410 u8 toggle_ul;
411 u16 token_dl;
412
413 struct tty_struct *tty;
414 int tty_open_count;
415 /* mutex to ensure one access patch to this port */
416 struct mutex tty_sem;
417 wait_queue_head_t tty_wait;
418 struct async_icount tty_icount;
419};
420
421/* Private data one for each card in the system */
422struct nozomi {
423 void __iomem *base_addr;
424 unsigned long flip;
425
426 /* Pointers to registers */
427 void __iomem *reg_iir;
428 void __iomem *reg_fcr;
429 void __iomem *reg_ier;
430
431 u16 last_ier;
432 enum card_type card_type;
433 struct config_table config_table; /* Configuration table */
434 struct pci_dev *pdev;
435 struct port port[NOZOMI_MAX_PORTS];
436 u8 *send_buf;
437
438 spinlock_t spin_mutex; /* secures access to registers and tty */
439
440 unsigned int index_start;
441 u32 open_ttys;
442};
443
444/* This is a data packet that is read or written to/from card */
445struct buffer {
446 u32 size; /* size is the length of the data buffer */
447 u8 *data;
448} __attribute__ ((packed));
449
450/* Global variables */
451static struct pci_device_id nozomi_pci_tbl[] = {
452 {PCI_DEVICE(VENDOR1, DEVICE1)},
453 {},
454};
455
456MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
457
458static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
459static struct tty_driver *ntty_driver;
460
461/*
462 * find card by tty_index
463 */
464static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
465{
466 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
467}
468
469static inline struct port *get_port_by_tty(const struct tty_struct *tty)
470{
471 struct nozomi *ndev = get_dc_by_tty(tty);
472 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
473}
474
475/*
476 * TODO:
477 * -Optimize
478 * -Rewrite cleaner
479 */
480
481static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
482 u32 size_bytes)
483{
484 u32 i = 0;
485 const u32 *ptr = (__force u32 *) mem_addr_start;
486 u16 *buf16;
487
488 if (unlikely(!ptr || !buf))
489 goto out;
490
491 /* shortcut for extremely often used cases */
492 switch (size_bytes) {
493 case 2: /* 2 bytes */
494 buf16 = (u16 *) buf;
495 *buf16 = __le16_to_cpu(readw((void __iomem *)ptr));
496 goto out;
497 break;
498 case 4: /* 4 bytes */
499 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
500 goto out;
501 break;
502 }
503
504 while (i < size_bytes) {
505 if (size_bytes - i == 2) {
506 /* Handle 2 bytes in the end */
507 buf16 = (u16 *) buf;
508 *(buf16) = __le16_to_cpu(readw((void __iomem *)ptr));
509 i += 2;
510 } else {
511 /* Read 4 bytes */
512 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
513 i += 4;
514 }
515 buf++;
516 ptr++;
517 }
518out:
519 return;
520}
521
522/*
523 * TODO:
524 * -Optimize
525 * -Rewrite cleaner
526 */
527static u32 write_mem32(void __iomem *mem_addr_start, u32 *buf,
528 u32 size_bytes)
529{
530 u32 i = 0;
531 u32 *ptr = (__force u32 *) mem_addr_start;
532 u16 *buf16;
533
534 if (unlikely(!ptr || !buf))
535 return 0;
536
537 /* shortcut for extremely often used cases */
538 switch (size_bytes) {
539 case 2: /* 2 bytes */
540 buf16 = (u16 *) buf;
541 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
542 return 2;
543 break;
544 case 1: /*
545 * also needs to write 4 bytes in this case
546 * so falling through..
547 */
548 case 4: /* 4 bytes */
549 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
550 return 4;
551 break;
552 }
553
554 while (i < size_bytes) {
555 if (size_bytes - i == 2) {
556 /* 2 bytes */
557 buf16 = (u16 *) buf;
558 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
559 i += 2;
560 } else {
561 /* 4 bytes */
562 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
563 i += 4;
564 }
565 buf++;
566 ptr++;
567 }
568 return i;
569}
570
571/* Setup pointers to different channels and also setup buffer sizes. */
572static void setup_memory(struct nozomi *dc)
573{
574 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
575 /* The length reported is including the length field of 4 bytes,
576 * hence subtract with 4.
577 */
578 const u16 buff_offset = 4;
579
580 /* Modem port dl configuration */
581 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
582 dc->port[PORT_MDM].dl_addr[CH_B] =
583 (offset += dc->config_table.dl_mdm_len1);
584 dc->port[PORT_MDM].dl_size[CH_A] =
585 dc->config_table.dl_mdm_len1 - buff_offset;
586 dc->port[PORT_MDM].dl_size[CH_B] =
587 dc->config_table.dl_mdm_len2 - buff_offset;
588
589 /* Diag port dl configuration */
590 dc->port[PORT_DIAG].dl_addr[CH_A] =
591 (offset += dc->config_table.dl_mdm_len2);
592 dc->port[PORT_DIAG].dl_size[CH_A] =
593 dc->config_table.dl_diag_len1 - buff_offset;
594 dc->port[PORT_DIAG].dl_addr[CH_B] =
595 (offset += dc->config_table.dl_diag_len1);
596 dc->port[PORT_DIAG].dl_size[CH_B] =
597 dc->config_table.dl_diag_len2 - buff_offset;
598
599 /* App1 port dl configuration */
600 dc->port[PORT_APP1].dl_addr[CH_A] =
601 (offset += dc->config_table.dl_diag_len2);
602 dc->port[PORT_APP1].dl_size[CH_A] =
603 dc->config_table.dl_app1_len - buff_offset;
604
605 /* App2 port dl configuration */
606 dc->port[PORT_APP2].dl_addr[CH_A] =
607 (offset += dc->config_table.dl_app1_len);
608 dc->port[PORT_APP2].dl_size[CH_A] =
609 dc->config_table.dl_app2_len - buff_offset;
610
611 /* Ctrl dl configuration */
612 dc->port[PORT_CTRL].dl_addr[CH_A] =
613 (offset += dc->config_table.dl_app2_len);
614 dc->port[PORT_CTRL].dl_size[CH_A] =
615 dc->config_table.dl_ctrl_len - buff_offset;
616
617 offset = dc->base_addr + dc->config_table.ul_start;
618
619 /* Modem Port ul configuration */
620 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
621 dc->port[PORT_MDM].ul_size[CH_A] =
622 dc->config_table.ul_mdm_len1 - buff_offset;
623 dc->port[PORT_MDM].ul_addr[CH_B] =
624 (offset += dc->config_table.ul_mdm_len1);
625 dc->port[PORT_MDM].ul_size[CH_B] =
626 dc->config_table.ul_mdm_len2 - buff_offset;
627
628 /* Diag port ul configuration */
629 dc->port[PORT_DIAG].ul_addr[CH_A] =
630 (offset += dc->config_table.ul_mdm_len2);
631 dc->port[PORT_DIAG].ul_size[CH_A] =
632 dc->config_table.ul_diag_len - buff_offset;
633
634 /* App1 port ul configuration */
635 dc->port[PORT_APP1].ul_addr[CH_A] =
636 (offset += dc->config_table.ul_diag_len);
637 dc->port[PORT_APP1].ul_size[CH_A] =
638 dc->config_table.ul_app1_len - buff_offset;
639
640 /* App2 port ul configuration */
641 dc->port[PORT_APP2].ul_addr[CH_A] =
642 (offset += dc->config_table.ul_app1_len);
643 dc->port[PORT_APP2].ul_size[CH_A] =
644 dc->config_table.ul_app2_len - buff_offset;
645
646 /* Ctrl ul configuration */
647 dc->port[PORT_CTRL].ul_addr[CH_A] =
648 (offset += dc->config_table.ul_app2_len);
649 dc->port[PORT_CTRL].ul_size[CH_A] =
650 dc->config_table.ul_ctrl_len - buff_offset;
651}
652
653/* Dump config table under initalization phase */
654#ifdef DEBUG
655static void dump_table(const struct nozomi *dc)
656{
657 DBG3("signature: 0x%08X", dc->config_table.signature);
658 DBG3("version: 0x%04X", dc->config_table.version);
659 DBG3("product_information: 0x%04X", \
660 dc->config_table.product_information);
661 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
662 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
663 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
664 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
665
666 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
667 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
668 dc->config_table.dl_mdm_len1);
669 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
670 dc->config_table.dl_mdm_len2);
671 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
672 dc->config_table.dl_diag_len1);
673 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
674 dc->config_table.dl_diag_len2);
675 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
676 dc->config_table.dl_app1_len);
677 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
678 dc->config_table.dl_app2_len);
679 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
680 dc->config_table.dl_ctrl_len);
681 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
682 dc->config_table.ul_start);
683 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
684 dc->config_table.ul_mdm_len1);
685 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
686 dc->config_table.ul_mdm_len2);
687 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
688 dc->config_table.ul_diag_len);
689 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
690 dc->config_table.ul_app1_len);
691 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
692 dc->config_table.ul_app2_len);
693 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
694 dc->config_table.ul_ctrl_len);
695}
696#else
697static __inline__ void dump_table(const struct nozomi *dc) { }
698#endif
699
700/*
701 * Read configuration table from card under intalization phase
702 * Returns 1 if ok, else 0
703 */
704static int nozomi_read_config_table(struct nozomi *dc)
705{
706 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
707 sizeof(struct config_table));
708
709 if (dc->config_table.signature != CONFIG_MAGIC) {
710 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
711 dc->config_table.signature, CONFIG_MAGIC);
712 return 0;
713 }
714
715 if ((dc->config_table.version == 0)
716 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
717 int i;
718 DBG1("Second phase, configuring card");
719
720 setup_memory(dc);
721
722 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
723 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
724 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
725 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
726 dc->port[PORT_MDM].toggle_ul,
727 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
728
729 dump_table(dc);
730
731 for (i = PORT_MDM; i < MAX_PORT; i++) {
732 dc->port[i].fifo_ul =
733 kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
734 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
735 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
736 }
737
738 /* Enable control channel */
739 dc->last_ier = dc->last_ier | CTRL_DL;
740 writew(dc->last_ier, dc->reg_ier);
741
742 dev_info(&dc->pdev->dev, "Initialization OK!\n");
743 return 1;
744 }
745
746 if ((dc->config_table.version > 0)
747 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
748 u32 offset = 0;
749 DBG1("First phase: pushing upload buffers, clearing download");
750
751 dev_info(&dc->pdev->dev, "Version of card: %d\n",
752 dc->config_table.version);
753
754 /* Here we should disable all I/O over F32. */
755 setup_memory(dc);
756
757 /*
758 * We should send ALL channel pair tokens back along
759 * with reset token
760 */
761
762 /* push upload modem buffers */
763 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
764 (u32 *) &offset, 4);
765 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
766 (u32 *) &offset, 4);
767
768 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
769
770 DBG1("First phase done");
771 }
772
773 return 1;
774}
775
776/* Enable uplink interrupts */
777static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
778{
779 u16 mask[NOZOMI_MAX_PORTS] = \
780 {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
781
782 if (port < NOZOMI_MAX_PORTS) {
783 dc->last_ier |= mask[port];
784 writew(dc->last_ier, dc->reg_ier);
785 } else {
786 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
787 }
788}
789
790/* Disable uplink interrupts */
791static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
792{
793 u16 mask[NOZOMI_MAX_PORTS] = \
794 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
795
796 if (port < NOZOMI_MAX_PORTS) {
797 dc->last_ier &= mask[port];
798 writew(dc->last_ier, dc->reg_ier);
799 } else {
800 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
801 }
802}
803
804/* Enable downlink interrupts */
805static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
806{
807 u16 mask[NOZOMI_MAX_PORTS] = \
808 {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
809
810 if (port < NOZOMI_MAX_PORTS) {
811 dc->last_ier |= mask[port];
812 writew(dc->last_ier, dc->reg_ier);
813 } else {
814 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
815 }
816}
817
818/* Disable downlink interrupts */
819static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
820{
821 u16 mask[NOZOMI_MAX_PORTS] = \
822 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
823
824 if (port < NOZOMI_MAX_PORTS) {
825 dc->last_ier &= mask[port];
826 writew(dc->last_ier, dc->reg_ier);
827 } else {
828 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
829 }
830}
831
832/*
833 * Return 1 - send buffer to card and ack.
834 * Return 0 - don't ack, don't send buffer to card.
835 */
836static int send_data(enum port_type index, struct nozomi *dc)
837{
838 u32 size = 0;
839 struct port *port = &dc->port[index];
840 u8 toggle = port->toggle_ul;
841 void __iomem *addr = port->ul_addr[toggle];
842 u32 ul_size = port->ul_size[toggle];
843 struct tty_struct *tty = port->tty;
844
845 /* Get data from tty and place in buf for now */
846 size = __kfifo_get(port->fifo_ul, dc->send_buf,
847 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
848
849 if (size == 0) {
850 DBG4("No more data to send, disable link:");
851 return 0;
852 }
853
854 /* DUMP(buf, size); */
855
856 /* Write length + data */
857 write_mem32(addr, (u32 *) &size, 4);
858 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
859
860 if (tty)
861 tty_wakeup(tty);
862
863 return 1;
864}
865
866/* If all data has been read, return 1, else 0 */
867static int receive_data(enum port_type index, struct nozomi *dc)
868{
869 u8 buf[RECEIVE_BUF_MAX] = { 0 };
870 int size;
871 u32 offset = 4;
872 struct port *port = &dc->port[index];
873 void __iomem *addr = port->dl_addr[port->toggle_dl];
874 struct tty_struct *tty = port->tty;
875 int i;
876
877 if (unlikely(!tty)) {
878 DBG1("tty not open for port: %d?", index);
879 return 1;
880 }
881
882 read_mem32((u32 *) &size, addr, 4);
883 /* DBG1( "%d bytes port: %d", size, index); */
884
885 if (test_bit(TTY_THROTTLED, &tty->flags)) {
886 DBG1("No room in tty, don't read data, don't ack interrupt, "
887 "disable interrupt");
888
889 /* disable interrupt in downlink... */
890 disable_transmit_dl(index, dc);
891 return 0;
892 }
893
894 if (unlikely(size == 0)) {
895 dev_err(&dc->pdev->dev, "size == 0?\n");
896 return 1;
897 }
898
899 tty_buffer_request_room(tty, size);
900
901 while (size > 0) {
902 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
903
904 if (size == 1) {
905 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
906 size = 0;
907 } else if (size < RECEIVE_BUF_MAX) {
908 size -= tty_insert_flip_string(tty, (char *) buf, size);
909 } else {
910 i = tty_insert_flip_string(tty, \
911 (char *) buf, RECEIVE_BUF_MAX);
912 size -= i;
913 offset += i;
914 }
915 }
916
917 set_bit(index, &dc->flip);
918
919 return 1;
920}
921
922/* Debug for interrupts */
923#ifdef DEBUG
924static char *interrupt2str(u16 interrupt)
925{
926 static char buf[TMP_BUF_MAX];
927 char *p = buf;
928
929 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
930 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
931 "MDM_DL2 ") : NULL;
932
933 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
934 "MDM_UL1 ") : NULL;
935 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
936 "MDM_UL2 ") : NULL;
937
938 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
939 "DIAG_DL1 ") : NULL;
940 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
941 "DIAG_DL2 ") : NULL;
942
943 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
944 "DIAG_UL ") : NULL;
945
946 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
947 "APP1_DL ") : NULL;
948 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
949 "APP2_DL ") : NULL;
950
951 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
952 "APP1_UL ") : NULL;
953 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
954 "APP2_UL ") : NULL;
955
956 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
957 "CTRL_DL ") : NULL;
958 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
959 "CTRL_UL ") : NULL;
960
961 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
962 "RESET ") : NULL;
963
964 return buf;
965}
966#endif
967
968/*
969 * Receive flow control
970 * Return 1 - If ok, else 0
971 */
972static int receive_flow_control(struct nozomi *dc)
973{
974 enum port_type port = PORT_MDM;
975 struct ctrl_dl ctrl_dl;
976 struct ctrl_dl old_ctrl;
977 u16 enable_ier = 0;
978
979 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
980
981 switch (ctrl_dl.port) {
982 case CTRL_CMD:
983 DBG1("The Base Band sends this value as a response to a "
984 "request for IMSI detach sent over the control "
985 "channel uplink (see section 7.6.1).");
986 break;
987 case CTRL_MDM:
988 port = PORT_MDM;
989 enable_ier = MDM_DL;
990 break;
991 case CTRL_DIAG:
992 port = PORT_DIAG;
993 enable_ier = DIAG_DL;
994 break;
995 case CTRL_APP1:
996 port = PORT_APP1;
997 enable_ier = APP1_DL;
998 break;
999 case CTRL_APP2:
1000 port = PORT_APP2;
1001 enable_ier = APP2_DL;
1002 break;
1003 default:
1004 dev_err(&dc->pdev->dev,
1005 "ERROR: flow control received for non-existing port\n");
1006 return 0;
1007 };
1008
1009 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
1010 *((u16 *)&ctrl_dl));
1011
1012 old_ctrl = dc->port[port].ctrl_dl;
1013 dc->port[port].ctrl_dl = ctrl_dl;
1014
1015 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
1016 DBG1("Disable interrupt (0x%04X) on port: %d",
1017 enable_ier, port);
1018 disable_transmit_ul(port, dc);
1019
1020 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
1021
1022 if (__kfifo_len(dc->port[port].fifo_ul)) {
1023 DBG1("Enable interrupt (0x%04X) on port: %d",
1024 enable_ier, port);
1025 DBG1("Data in buffer [%d], enable transmit! ",
1026 __kfifo_len(dc->port[port].fifo_ul));
1027 enable_transmit_ul(port, dc);
1028 } else {
1029 DBG1("No data in buffer...");
1030 }
1031 }
1032
1033 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1034 DBG1(" No change in mctrl");
1035 return 1;
1036 }
1037 /* Update statistics */
1038 if (old_ctrl.CTS != ctrl_dl.CTS)
1039 dc->port[port].tty_icount.cts++;
1040 if (old_ctrl.DSR != ctrl_dl.DSR)
1041 dc->port[port].tty_icount.dsr++;
1042 if (old_ctrl.RI != ctrl_dl.RI)
1043 dc->port[port].tty_icount.rng++;
1044 if (old_ctrl.DCD != ctrl_dl.DCD)
1045 dc->port[port].tty_icount.dcd++;
1046
1047 wake_up_interruptible(&dc->port[port].tty_wait);
1048
1049 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1050 port,
1051 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1052 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1053
1054 return 1;
1055}
1056
1057static enum ctrl_port_type port2ctrl(enum port_type port,
1058 const struct nozomi *dc)
1059{
1060 switch (port) {
1061 case PORT_MDM:
1062 return CTRL_MDM;
1063 case PORT_DIAG:
1064 return CTRL_DIAG;
1065 case PORT_APP1:
1066 return CTRL_APP1;
1067 case PORT_APP2:
1068 return CTRL_APP2;
1069 default:
1070 dev_err(&dc->pdev->dev,
1071 "ERROR: send flow control " \
1072 "received for non-existing port\n");
1073 };
1074 return CTRL_ERROR;
1075}
1076
1077/*
1078 * Send flow control, can only update one channel at a time
1079 * Return 0 - If we have updated all flow control
1080 * Return 1 - If we need to update more flow control, ack current enable more
1081 */
1082static int send_flow_control(struct nozomi *dc)
1083{
1084 u32 i, more_flow_control_to_be_updated = 0;
1085 u16 *ctrl;
1086
1087 for (i = PORT_MDM; i < MAX_PORT; i++) {
1088 if (dc->port[i].update_flow_control) {
1089 if (more_flow_control_to_be_updated) {
1090 /* We have more flow control to be updated */
1091 return 1;
1092 }
1093 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1094 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1095 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1096 (u32 *) ctrl, 2);
1097 dc->port[i].update_flow_control = 0;
1098 more_flow_control_to_be_updated = 1;
1099 }
1100 }
1101 return 0;
1102}
1103
1104/*
1105 * Handle donlink data, ports that are handled are modem and diagnostics
1106 * Return 1 - ok
1107 * Return 0 - toggle fields are out of sync
1108 */
1109static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1110 u16 read_iir, u16 mask1, u16 mask2)
1111{
1112 if (*toggle == 0 && read_iir & mask1) {
1113 if (receive_data(port, dc)) {
1114 writew(mask1, dc->reg_fcr);
1115 *toggle = !(*toggle);
1116 }
1117
1118 if (read_iir & mask2) {
1119 if (receive_data(port, dc)) {
1120 writew(mask2, dc->reg_fcr);
1121 *toggle = !(*toggle);
1122 }
1123 }
1124 } else if (*toggle == 1 && read_iir & mask2) {
1125 if (receive_data(port, dc)) {
1126 writew(mask2, dc->reg_fcr);
1127 *toggle = !(*toggle);
1128 }
1129
1130 if (read_iir & mask1) {
1131 if (receive_data(port, dc)) {
1132 writew(mask1, dc->reg_fcr);
1133 *toggle = !(*toggle);
1134 }
1135 }
1136 } else {
1137 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1138 *toggle);
1139 return 0;
1140 }
1141 return 1;
1142}
1143
1144/*
1145 * Handle uplink data, this is currently for the modem port
1146 * Return 1 - ok
1147 * Return 0 - toggle field are out of sync
1148 */
1149static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1150{
1151 u8 *toggle = &(dc->port[port].toggle_ul);
1152
1153 if (*toggle == 0 && read_iir & MDM_UL1) {
1154 dc->last_ier &= ~MDM_UL;
1155 writew(dc->last_ier, dc->reg_ier);
1156 if (send_data(port, dc)) {
1157 writew(MDM_UL1, dc->reg_fcr);
1158 dc->last_ier = dc->last_ier | MDM_UL;
1159 writew(dc->last_ier, dc->reg_ier);
1160 *toggle = !*toggle;
1161 }
1162
1163 if (read_iir & MDM_UL2) {
1164 dc->last_ier &= ~MDM_UL;
1165 writew(dc->last_ier, dc->reg_ier);
1166 if (send_data(port, dc)) {
1167 writew(MDM_UL2, dc->reg_fcr);
1168 dc->last_ier = dc->last_ier | MDM_UL;
1169 writew(dc->last_ier, dc->reg_ier);
1170 *toggle = !*toggle;
1171 }
1172 }
1173
1174 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1175 dc->last_ier &= ~MDM_UL;
1176 writew(dc->last_ier, dc->reg_ier);
1177 if (send_data(port, dc)) {
1178 writew(MDM_UL2, dc->reg_fcr);
1179 dc->last_ier = dc->last_ier | MDM_UL;
1180 writew(dc->last_ier, dc->reg_ier);
1181 *toggle = !*toggle;
1182 }
1183
1184 if (read_iir & MDM_UL1) {
1185 dc->last_ier &= ~MDM_UL;
1186 writew(dc->last_ier, dc->reg_ier);
1187 if (send_data(port, dc)) {
1188 writew(MDM_UL1, dc->reg_fcr);
1189 dc->last_ier = dc->last_ier | MDM_UL;
1190 writew(dc->last_ier, dc->reg_ier);
1191 *toggle = !*toggle;
1192 }
1193 }
1194 } else {
1195 writew(read_iir & MDM_UL, dc->reg_fcr);
1196 dev_err(&dc->pdev->dev, "port out of sync!\n");
1197 return 0;
1198 }
1199 return 1;
1200}
1201
1202static irqreturn_t interrupt_handler(int irq, void *dev_id)
1203{
1204 struct nozomi *dc = dev_id;
1205 unsigned int a;
1206 u16 read_iir;
1207
1208 if (!dc)
1209 return IRQ_NONE;
1210
1211 spin_lock(&dc->spin_mutex);
1212 read_iir = readw(dc->reg_iir);
1213
1214 /* Card removed */
1215 if (read_iir == (u16)-1)
1216 goto none;
1217 /*
1218 * Just handle interrupt enabled in IER
1219 * (by masking with dc->last_ier)
1220 */
1221 read_iir &= dc->last_ier;
1222
1223 if (read_iir == 0)
1224 goto none;
1225
1226
1227 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1228 dc->last_ier);
1229
1230 if (read_iir & RESET) {
1231 if (unlikely(!nozomi_read_config_table(dc))) {
1232 dc->last_ier = 0x0;
1233 writew(dc->last_ier, dc->reg_ier);
1234 dev_err(&dc->pdev->dev, "Could not read status from "
1235 "card, we should disable interface\n");
1236 } else {
1237 writew(RESET, dc->reg_fcr);
1238 }
1239 /* No more useful info if this was the reset interrupt. */
1240 goto exit_handler;
1241 }
1242 if (read_iir & CTRL_UL) {
1243 DBG1("CTRL_UL");
1244 dc->last_ier &= ~CTRL_UL;
1245 writew(dc->last_ier, dc->reg_ier);
1246 if (send_flow_control(dc)) {
1247 writew(CTRL_UL, dc->reg_fcr);
1248 dc->last_ier = dc->last_ier | CTRL_UL;
1249 writew(dc->last_ier, dc->reg_ier);
1250 }
1251 }
1252 if (read_iir & CTRL_DL) {
1253 receive_flow_control(dc);
1254 writew(CTRL_DL, dc->reg_fcr);
1255 }
1256 if (read_iir & MDM_DL) {
1257 if (!handle_data_dl(dc, PORT_MDM,
1258 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1259 MDM_DL1, MDM_DL2)) {
1260 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1261 goto exit_handler;
1262 }
1263 }
1264 if (read_iir & MDM_UL) {
1265 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1266 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1267 goto exit_handler;
1268 }
1269 }
1270 if (read_iir & DIAG_DL) {
1271 if (!handle_data_dl(dc, PORT_DIAG,
1272 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1273 DIAG_DL1, DIAG_DL2)) {
1274 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1275 goto exit_handler;
1276 }
1277 }
1278 if (read_iir & DIAG_UL) {
1279 dc->last_ier &= ~DIAG_UL;
1280 writew(dc->last_ier, dc->reg_ier);
1281 if (send_data(PORT_DIAG, dc)) {
1282 writew(DIAG_UL, dc->reg_fcr);
1283 dc->last_ier = dc->last_ier | DIAG_UL;
1284 writew(dc->last_ier, dc->reg_ier);
1285 }
1286 }
1287 if (read_iir & APP1_DL) {
1288 if (receive_data(PORT_APP1, dc))
1289 writew(APP1_DL, dc->reg_fcr);
1290 }
1291 if (read_iir & APP1_UL) {
1292 dc->last_ier &= ~APP1_UL;
1293 writew(dc->last_ier, dc->reg_ier);
1294 if (send_data(PORT_APP1, dc)) {
1295 writew(APP1_UL, dc->reg_fcr);
1296 dc->last_ier = dc->last_ier | APP1_UL;
1297 writew(dc->last_ier, dc->reg_ier);
1298 }
1299 }
1300 if (read_iir & APP2_DL) {
1301 if (receive_data(PORT_APP2, dc))
1302 writew(APP2_DL, dc->reg_fcr);
1303 }
1304 if (read_iir & APP2_UL) {
1305 dc->last_ier &= ~APP2_UL;
1306 writew(dc->last_ier, dc->reg_ier);
1307 if (send_data(PORT_APP2, dc)) {
1308 writew(APP2_UL, dc->reg_fcr);
1309 dc->last_ier = dc->last_ier | APP2_UL;
1310 writew(dc->last_ier, dc->reg_ier);
1311 }
1312 }
1313
1314exit_handler:
1315 spin_unlock(&dc->spin_mutex);
1316 for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1317 if (test_and_clear_bit(a, &dc->flip))
1318 tty_flip_buffer_push(dc->port[a].tty);
1319 return IRQ_HANDLED;
1320none:
1321 spin_unlock(&dc->spin_mutex);
1322 return IRQ_NONE;
1323}
1324
1325static void nozomi_get_card_type(struct nozomi *dc)
1326{
1327 int i;
1328 u32 size = 0;
1329
1330 for (i = 0; i < 6; i++)
1331 size += pci_resource_len(dc->pdev, i);
1332
1333 /* Assume card type F32_8 if no match */
1334 dc->card_type = size == 2048 ? F32_2 : F32_8;
1335
1336 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1337}
1338
1339static void nozomi_setup_private_data(struct nozomi *dc)
1340{
1341 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1342 unsigned int i;
1343
1344 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1345 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1346 dc->reg_ier = (void __iomem *)(offset + R_IER);
1347 dc->last_ier = 0;
1348 dc->flip = 0;
1349
1350 dc->port[PORT_MDM].token_dl = MDM_DL;
1351 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1352 dc->port[PORT_APP1].token_dl = APP1_DL;
1353 dc->port[PORT_APP2].token_dl = APP2_DL;
1354
1355 for (i = 0; i < MAX_PORT; i++)
1356 init_waitqueue_head(&dc->port[i].tty_wait);
1357}
1358
1359static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1360 char *buf)
1361{
1362 struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1363
1364 return sprintf(buf, "%d\n", dc->card_type);
1365}
1366static DEVICE_ATTR(card_type, 0444, card_type_show, NULL);
1367
1368static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1369 char *buf)
1370{
1371 struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1372
1373 return sprintf(buf, "%u\n", dc->open_ttys);
1374}
1375static DEVICE_ATTR(open_ttys, 0444, open_ttys_show, NULL);
1376
1377static void make_sysfs_files(struct nozomi *dc)
1378{
1379 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1380 dev_err(&dc->pdev->dev,
1381 "Could not create sysfs file for card_type\n");
1382 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1383 dev_err(&dc->pdev->dev,
1384 "Could not create sysfs file for open_ttys\n");
1385}
1386
1387static void remove_sysfs_files(struct nozomi *dc)
1388{
1389 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1390 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1391}
1392
1393/* Allocate memory for one device */
1394static int __devinit nozomi_card_init(struct pci_dev *pdev,
1395 const struct pci_device_id *ent)
1396{
1397 resource_size_t start;
1398 int ret;
1399 struct nozomi *dc = NULL;
1400 int ndev_idx;
1401 int i;
1402
1403 dev_dbg(&pdev->dev, "Init, new card found\n");
1404
1405 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1406 if (!ndevs[ndev_idx])
1407 break;
1408
1409 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1410 dev_err(&pdev->dev, "no free tty range for this card left\n");
1411 ret = -EIO;
1412 goto err;
1413 }
1414
1415 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1416 if (unlikely(!dc)) {
1417 dev_err(&pdev->dev, "Could not allocate memory\n");
1418 ret = -ENOMEM;
1419 goto err_free;
1420 }
1421
1422 dc->pdev = pdev;
1423
1424 /* Find out what card type it is */
1425 nozomi_get_card_type(dc);
1426
1427 ret = pci_enable_device(dc->pdev);
1428 if (ret) {
1429 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1430 goto err_free;
1431 }
1432
1433 start = pci_resource_start(dc->pdev, 0);
1434 if (start == 0) {
1435 dev_err(&pdev->dev, "No I/O address for card detected\n");
1436 ret = -ENODEV;
1437 goto err_disable_device;
1438 }
1439
1440 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1441 if (ret) {
1442 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1443 (int) /* nozomi_private.io_addr */ 0);
1444 goto err_disable_device;
1445 }
1446
1447 dc->base_addr = ioremap(start, dc->card_type);
1448 if (!dc->base_addr) {
1449 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1450 ret = -ENODEV;
1451 goto err_rel_regs;
1452 }
1453
1454 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1455 if (!dc->send_buf) {
1456 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1457 ret = -ENOMEM;
1458 goto err_free_sbuf;
1459 }
1460
1461 spin_lock_init(&dc->spin_mutex);
1462
1463 nozomi_setup_private_data(dc);
1464
1465 /* Disable all interrupts */
1466 dc->last_ier = 0;
1467 writew(dc->last_ier, dc->reg_ier);
1468
1469 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1470 NOZOMI_NAME, dc);
1471 if (unlikely(ret)) {
1472 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1473 goto err_free_sbuf;
1474 }
1475
1476 DBG1("base_addr: %p", dc->base_addr);
1477
1478 make_sysfs_files(dc);
1479
1480 dc->index_start = ndev_idx * MAX_PORT;
1481 ndevs[ndev_idx] = dc;
1482
1483 for (i = 0; i < MAX_PORT; i++) {
1484 mutex_init(&dc->port[i].tty_sem);
1485 dc->port[i].tty_open_count = 0;
1486 dc->port[i].tty = NULL;
1487 tty_register_device(ntty_driver, dc->index_start + i,
1488 &pdev->dev);
1489 }
1490
1491 /* Enable RESET interrupt. */
1492 dc->last_ier = RESET;
1493 writew(dc->last_ier, dc->reg_ier);
1494
1495 pci_set_drvdata(pdev, dc);
1496
1497 return 0;
1498
1499err_free_sbuf:
1500 kfree(dc->send_buf);
1501 iounmap(dc->base_addr);
1502err_rel_regs:
1503 pci_release_regions(pdev);
1504err_disable_device:
1505 pci_disable_device(pdev);
1506err_free:
1507 kfree(dc);
1508err:
1509 return ret;
1510}
1511
1512static void __devexit tty_exit(struct nozomi *dc)
1513{
1514 unsigned int i;
1515
1516 DBG1(" ");
1517
1518 flush_scheduled_work();
1519
1520 for (i = 0; i < MAX_PORT; ++i)
1521 if (dc->port[i].tty && \
1522 list_empty(&dc->port[i].tty->hangup_work.entry))
1523 tty_hangup(dc->port[i].tty);
1524
1525 while (dc->open_ttys)
1526 msleep(1);
1527
1528 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1529 tty_unregister_device(ntty_driver, i);
1530}
1531
1532/* Deallocate memory for one device */
1533static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1534{
1535 int i;
1536 struct ctrl_ul ctrl;
1537 struct nozomi *dc = pci_get_drvdata(pdev);
1538
1539 /* Disable all interrupts */
1540 dc->last_ier = 0;
1541 writew(dc->last_ier, dc->reg_ier);
1542
1543 tty_exit(dc);
1544
1545 /* Send 0x0001, command card to resend the reset token. */
1546 /* This is to get the reset when the module is reloaded. */
1547 ctrl.port = 0x00;
1548 ctrl.reserved = 0;
1549 ctrl.RTS = 0;
1550 ctrl.DTR = 1;
1551 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1552
1553 /* Setup dc->reg addresses to we can use defines here */
1554 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1555 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1556
1557 remove_sysfs_files(dc);
1558
1559 free_irq(pdev->irq, dc);
1560
1561 for (i = 0; i < MAX_PORT; i++)
1562 if (dc->port[i].fifo_ul)
1563 kfifo_free(dc->port[i].fifo_ul);
1564
1565 kfree(dc->send_buf);
1566
1567 iounmap(dc->base_addr);
1568
1569 pci_release_regions(pdev);
1570
1571 pci_disable_device(pdev);
1572
1573 ndevs[dc->index_start / MAX_PORT] = NULL;
1574
1575 kfree(dc);
1576}
1577
1578static void set_rts(const struct tty_struct *tty, int rts)
1579{
1580 struct port *port = get_port_by_tty(tty);
1581
1582 port->ctrl_ul.RTS = rts;
1583 port->update_flow_control = 1;
1584 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1585}
1586
1587static void set_dtr(const struct tty_struct *tty, int dtr)
1588{
1589 struct port *port = get_port_by_tty(tty);
1590
1591 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1592
1593 port->ctrl_ul.DTR = dtr;
1594 port->update_flow_control = 1;
1595 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1596}
1597
1598/*
1599 * ----------------------------------------------------------------------------
1600 * TTY code
1601 * ----------------------------------------------------------------------------
1602 */
1603
1604/* Called when the userspace process opens the tty, /dev/noz*. */
1605static int ntty_open(struct tty_struct *tty, struct file *file)
1606{
1607 struct port *port = get_port_by_tty(tty);
1608 struct nozomi *dc = get_dc_by_tty(tty);
1609 unsigned long flags;
1610
1611 if (!port || !dc)
1612 return -ENODEV;
1613
1614 if (mutex_lock_interruptible(&port->tty_sem))
1615 return -ERESTARTSYS;
1616
1617 port->tty_open_count++;
1618 dc->open_ttys++;
1619
1620 /* Enable interrupt downlink for channel */
1621 if (port->tty_open_count == 1) {
1622 tty->low_latency = 1;
1623 tty->driver_data = port;
1624 port->tty = tty;
1625 DBG1("open: %d", port->token_dl);
1626 spin_lock_irqsave(&dc->spin_mutex, flags);
1627 dc->last_ier = dc->last_ier | port->token_dl;
1628 writew(dc->last_ier, dc->reg_ier);
1629 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1630 }
1631
1632 mutex_unlock(&port->tty_sem);
1633
1634 return 0;
1635}
1636
1637/* Called when the userspace process close the tty, /dev/noz*. */
1638static void ntty_close(struct tty_struct *tty, struct file *file)
1639{
1640 struct nozomi *dc = get_dc_by_tty(tty);
1641 struct port *port = tty->driver_data;
1642 unsigned long flags;
1643
1644 if (!dc || !port)
1645 return;
1646
1647 if (mutex_lock_interruptible(&port->tty_sem))
1648 return;
1649
1650 if (!port->tty_open_count)
1651 goto exit;
1652
1653 dc->open_ttys--;
1654 port->tty_open_count--;
1655
1656 if (port->tty_open_count == 0) {
1657 DBG1("close: %d", port->token_dl);
1658 spin_lock_irqsave(&dc->spin_mutex, flags);
1659 dc->last_ier &= ~(port->token_dl);
1660 writew(dc->last_ier, dc->reg_ier);
1661 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1662 }
1663
1664exit:
1665 mutex_unlock(&port->tty_sem);
1666}
1667
1668/*
1669 * called when the userspace process writes to the tty (/dev/noz*).
1670 * Data is inserted into a fifo, which is then read and transfered to the modem.
1671 */
1672static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1673 int count)
1674{
1675 int rval = -EINVAL;
1676 struct nozomi *dc = get_dc_by_tty(tty);
1677 struct port *port = tty->driver_data;
1678 unsigned long flags;
1679
1680 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1681
1682 if (!dc || !port)
1683 return -ENODEV;
1684
1685 if (unlikely(!mutex_trylock(&port->tty_sem))) {
1686 /*
1687 * must test lock as tty layer wraps calls
1688 * to this function with BKL
1689 */
1690 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1691 "return EAGAIN\n");
1692 return -EAGAIN;
1693 }
1694
1695 if (unlikely(!port->tty_open_count)) {
1696 DBG1(" ");
1697 goto exit;
1698 }
1699
1700 rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1701
1702 /* notify card */
1703 if (unlikely(dc == NULL)) {
1704 DBG1("No device context?");
1705 goto exit;
1706 }
1707
1708 spin_lock_irqsave(&dc->spin_mutex, flags);
1709 /* CTS is only valid on the modem channel */
1710 if (port == &(dc->port[PORT_MDM])) {
1711 if (port->ctrl_dl.CTS) {
1712 DBG4("Enable interrupt");
1713 enable_transmit_ul(tty->index % MAX_PORT, dc);
1714 } else {
1715 dev_err(&dc->pdev->dev,
1716 "CTS not active on modem port?\n");
1717 }
1718 } else {
1719 enable_transmit_ul(tty->index % MAX_PORT, dc);
1720 }
1721 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1722
1723exit:
1724 mutex_unlock(&port->tty_sem);
1725 return rval;
1726}
1727
1728/*
1729 * Calculate how much is left in device
1730 * This method is called by the upper tty layer.
1731 * #according to sources N_TTY.c it expects a value >= 0 and
1732 * does not check for negative values.
1733 */
1734static int ntty_write_room(struct tty_struct *tty)
1735{
1736 struct port *port = tty->driver_data;
1737 int room = 0;
1738 struct nozomi *dc = get_dc_by_tty(tty);
1739
1740 if (!dc || !port)
1741 return 0;
1742 if (!mutex_trylock(&port->tty_sem))
1743 return 0;
1744
1745 if (!port->tty_open_count)
1746 goto exit;
1747
1748 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1749
1750exit:
1751 mutex_unlock(&port->tty_sem);
1752 return room;
1753}
1754
1755/* Gets io control parameters */
1756static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1757{
1758 struct port *port = tty->driver_data;
1759 struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1760 struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1761
1762 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1763 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1764 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1765 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1766 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1767 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1768}
1769
1770/* Sets io controls parameters */
1771static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1772 unsigned int set, unsigned int clear)
1773{
1774 if (set & TIOCM_RTS)
1775 set_rts(tty, 1);
1776 else if (clear & TIOCM_RTS)
1777 set_rts(tty, 0);
1778
1779 if (set & TIOCM_DTR)
1780 set_dtr(tty, 1);
1781 else if (clear & TIOCM_DTR)
1782 set_dtr(tty, 0);
1783
1784 return 0;
1785}
1786
1787static int ntty_cflags_changed(struct port *port, unsigned long flags,
1788 struct async_icount *cprev)
1789{
1790 struct async_icount cnow = port->tty_icount;
1791 int ret;
1792
1793 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1794 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1795 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1796 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1797
1798 *cprev = cnow;
1799
1800 return ret;
1801}
1802
1803static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1804{
1805 struct async_icount cnow = port->tty_icount;
1806 struct serial_icounter_struct icount;
1807
1808 icount.cts = cnow.cts;
1809 icount.dsr = cnow.dsr;
1810 icount.rng = cnow.rng;
1811 icount.dcd = cnow.dcd;
1812 icount.rx = cnow.rx;
1813 icount.tx = cnow.tx;
1814 icount.frame = cnow.frame;
1815 icount.overrun = cnow.overrun;
1816 icount.parity = cnow.parity;
1817 icount.brk = cnow.brk;
1818 icount.buf_overrun = cnow.buf_overrun;
1819
1820 return copy_to_user(argp, &icount, sizeof(icount));
1821}
1822
1823static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1824 unsigned int cmd, unsigned long arg)
1825{
1826 struct port *port = tty->driver_data;
1827 void __user *argp = (void __user *)arg;
1828 int rval = -ENOIOCTLCMD;
1829
1830 DBG1("******** IOCTL, cmd: %d", cmd);
1831
1832 switch (cmd) {
1833 case TIOCMIWAIT: {
1834 struct async_icount cprev = port->tty_icount;
1835
1836 rval = wait_event_interruptible(port->tty_wait,
1837 ntty_cflags_changed(port, arg, &cprev));
1838 break;
1839 } case TIOCGICOUNT:
1840 rval = ntty_ioctl_tiocgicount(port, argp);
1841 break;
1842 default:
1843 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1844 break;
1845 };
1846
1847 return rval;
1848}
1849
1850/*
1851 * Called by the upper tty layer when tty buffers are ready
1852 * to receive data again after a call to throttle.
1853 */
1854static void ntty_unthrottle(struct tty_struct *tty)
1855{
1856 struct nozomi *dc = get_dc_by_tty(tty);
1857 unsigned long flags;
1858
1859 DBG1("UNTHROTTLE");
1860 spin_lock_irqsave(&dc->spin_mutex, flags);
1861 enable_transmit_dl(tty->index % MAX_PORT, dc);
1862 set_rts(tty, 1);
1863
1864 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1865}
1866
1867/*
1868 * Called by the upper tty layer when the tty buffers are almost full.
1869 * The driver should stop send more data.
1870 */
1871static void ntty_throttle(struct tty_struct *tty)
1872{
1873 struct nozomi *dc = get_dc_by_tty(tty);
1874 unsigned long flags;
1875
1876 DBG1("THROTTLE");
1877 spin_lock_irqsave(&dc->spin_mutex, flags);
1878 set_rts(tty, 0);
1879 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1880}
1881
1882/* just to discard single character writes */
1883static void ntty_put_char(struct tty_struct *tty, unsigned char c)
1884{
1885 /* FIXME !!! */
1886 DBG2("PUT CHAR Function: %c", c);
1887}
1888
1889/* Returns number of chars in buffer, called by tty layer */
1890static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1891{
1892 struct port *port = tty->driver_data;
1893 struct nozomi *dc = get_dc_by_tty(tty);
1894 s32 rval;
1895
1896 if (unlikely(!dc || !port)) {
1897 rval = -ENODEV;
1898 goto exit_in_buffer;
1899 }
1900
1901 if (unlikely(!port->tty_open_count)) {
1902 dev_err(&dc->pdev->dev, "No tty open?\n");
1903 rval = -ENODEV;
1904 goto exit_in_buffer;
1905 }
1906
1907 rval = __kfifo_len(port->fifo_ul);
1908
1909exit_in_buffer:
1910 return rval;
1911}
1912
1913static struct tty_operations tty_ops = {
1914 .ioctl = ntty_ioctl,
1915 .open = ntty_open,
1916 .close = ntty_close,
1917 .write = ntty_write,
1918 .write_room = ntty_write_room,
1919 .unthrottle = ntty_unthrottle,
1920 .throttle = ntty_throttle,
1921 .chars_in_buffer = ntty_chars_in_buffer,
1922 .put_char = ntty_put_char,
1923 .tiocmget = ntty_tiocmget,
1924 .tiocmset = ntty_tiocmset,
1925};
1926
1927/* Module initialization */
1928static struct pci_driver nozomi_driver = {
1929 .name = NOZOMI_NAME,
1930 .id_table = nozomi_pci_tbl,
1931 .probe = nozomi_card_init,
1932 .remove = __devexit_p(nozomi_card_exit),
1933};
1934
1935static __init int nozomi_init(void)
1936{
1937 int ret;
1938
1939 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1940
1941 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1942 if (!ntty_driver)
1943 return -ENOMEM;
1944
1945 ntty_driver->owner = THIS_MODULE;
1946 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1947 ntty_driver->name = "noz";
1948 ntty_driver->major = 0;
1949 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1950 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1951 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1952 ntty_driver->init_termios = tty_std_termios;
1953 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1954 HUPCL | CLOCAL;
1955 ntty_driver->init_termios.c_ispeed = 115200;
1956 ntty_driver->init_termios.c_ospeed = 115200;
1957 tty_set_operations(ntty_driver, &tty_ops);
1958
1959 ret = tty_register_driver(ntty_driver);
1960 if (ret) {
1961 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1962 goto free_tty;
1963 }
1964
1965 ret = pci_register_driver(&nozomi_driver);
1966 if (ret) {
1967 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1968 goto unr_tty;
1969 }
1970
1971 return 0;
1972unr_tty:
1973 tty_unregister_driver(ntty_driver);
1974free_tty:
1975 put_tty_driver(ntty_driver);
1976 return ret;
1977}
1978
1979static __exit void nozomi_exit(void)
1980{
1981 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1982 pci_unregister_driver(&nozomi_driver);
1983 tty_unregister_driver(ntty_driver);
1984 put_tty_driver(ntty_driver);
1985}
1986
1987module_init(nozomi_init);
1988module_exit(nozomi_exit);
1989
1990module_param(debug, int, S_IRUGO | S_IWUSR);
1991
1992MODULE_LICENSE("Dual BSD/GPL");
1993MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 79581fab82d6..5efd5550f4ca 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -828,11 +828,8 @@ static int cpufreq_add_dev (struct sys_device * sys_dev)
828 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); 828 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
829 829
830 /* prepare interface data */ 830 /* prepare interface data */
831 policy->kobj.parent = &sys_dev->kobj; 831 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, &sys_dev->kobj,
832 policy->kobj.ktype = &ktype_cpufreq; 832 "cpufreq");
833 kobject_set_name(&policy->kobj, "cpufreq");
834
835 ret = kobject_register(&policy->kobj);
836 if (ret) { 833 if (ret) {
837 unlock_policy_rwsem_write(cpu); 834 unlock_policy_rwsem_write(cpu);
838 goto err_out_driver_exit; 835 goto err_out_driver_exit;
@@ -902,6 +899,7 @@ static int cpufreq_add_dev (struct sys_device * sys_dev)
902 goto err_out_unregister; 899 goto err_out_unregister;
903 } 900 }
904 901
902 kobject_uevent(&policy->kobj, KOBJ_ADD);
905 module_put(cpufreq_driver->owner); 903 module_put(cpufreq_driver->owner);
906 dprintk("initialization complete\n"); 904 dprintk("initialization complete\n");
907 cpufreq_debug_enable_ratelimit(); 905 cpufreq_debug_enable_ratelimit();
@@ -915,7 +913,7 @@ err_out_unregister:
915 cpufreq_cpu_data[j] = NULL; 913 cpufreq_cpu_data[j] = NULL;
916 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 914 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
917 915
918 kobject_unregister(&policy->kobj); 916 kobject_put(&policy->kobj);
919 wait_for_completion(&policy->kobj_unregister); 917 wait_for_completion(&policy->kobj_unregister);
920 918
921err_out_driver_exit: 919err_out_driver_exit:
@@ -1032,8 +1030,6 @@ static int __cpufreq_remove_dev (struct sys_device * sys_dev)
1032 1030
1033 unlock_policy_rwsem_write(cpu); 1031 unlock_policy_rwsem_write(cpu);
1034 1032
1035 kobject_unregister(&data->kobj);
1036
1037 kobject_put(&data->kobj); 1033 kobject_put(&data->kobj);
1038 1034
1039 /* we need to make sure that the underlying kobj is actually 1035 /* we need to make sure that the underlying kobj is actually
diff --git a/drivers/cpuidle/sysfs.c b/drivers/cpuidle/sysfs.c
index 0f3515e77d4b..088ea74edd34 100644
--- a/drivers/cpuidle/sysfs.c
+++ b/drivers/cpuidle/sysfs.c
@@ -277,7 +277,7 @@ static struct kobj_type ktype_state_cpuidle = {
277 277
278static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i) 278static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
279{ 279{
280 kobject_unregister(&device->kobjs[i]->kobj); 280 kobject_put(&device->kobjs[i]->kobj);
281 wait_for_completion(&device->kobjs[i]->kobj_unregister); 281 wait_for_completion(&device->kobjs[i]->kobj_unregister);
282 kfree(device->kobjs[i]); 282 kfree(device->kobjs[i]);
283 device->kobjs[i] = NULL; 283 device->kobjs[i] = NULL;
@@ -300,14 +300,13 @@ int cpuidle_add_state_sysfs(struct cpuidle_device *device)
300 kobj->state = &device->states[i]; 300 kobj->state = &device->states[i];
301 init_completion(&kobj->kobj_unregister); 301 init_completion(&kobj->kobj_unregister);
302 302
303 kobj->kobj.parent = &device->kobj; 303 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
304 kobj->kobj.ktype = &ktype_state_cpuidle; 304 "state%d", i);
305 kobject_set_name(&kobj->kobj, "state%d", i);
306 ret = kobject_register(&kobj->kobj);
307 if (ret) { 305 if (ret) {
308 kfree(kobj); 306 kfree(kobj);
309 goto error_state; 307 goto error_state;
310 } 308 }
309 kobject_uevent(&kobj->kobj, KOBJ_ADD);
311 device->kobjs[i] = kobj; 310 device->kobjs[i] = kobj;
312 } 311 }
313 312
@@ -339,12 +338,14 @@ int cpuidle_add_sysfs(struct sys_device *sysdev)
339{ 338{
340 int cpu = sysdev->id; 339 int cpu = sysdev->id;
341 struct cpuidle_device *dev; 340 struct cpuidle_device *dev;
341 int error;
342 342
343 dev = per_cpu(cpuidle_devices, cpu); 343 dev = per_cpu(cpuidle_devices, cpu);
344 dev->kobj.parent = &sysdev->kobj; 344 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
345 dev->kobj.ktype = &ktype_cpuidle; 345 "cpuidle");
346 kobject_set_name(&dev->kobj, "%s", "cpuidle"); 346 if (!error)
347 return kobject_register(&dev->kobj); 347 kobject_uevent(&dev->kobj, KOBJ_ADD);
348 return error;
348} 349}
349 350
350/** 351/**
@@ -357,5 +358,5 @@ void cpuidle_remove_sysfs(struct sys_device *sysdev)
357 struct cpuidle_device *dev; 358 struct cpuidle_device *dev;
358 359
359 dev = per_cpu(cpuidle_devices, cpu); 360 dev = per_cpu(cpuidle_devices, cpu);
360 kobject_unregister(&dev->kobj); 361 kobject_put(&dev->kobj);
361} 362}
diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
index d59b2f417306..bcf52df30339 100644
--- a/drivers/dma/dmaengine.c
+++ b/drivers/dma/dmaengine.c
@@ -41,12 +41,12 @@
41 * the definition of dma_event_callback in dmaengine.h. 41 * the definition of dma_event_callback in dmaengine.h.
42 * 42 *
43 * Each device has a kref, which is initialized to 1 when the device is 43 * Each device has a kref, which is initialized to 1 when the device is
44 * registered. A kref_get is done for each class_device registered. When the 44 * registered. A kref_get is done for each device registered. When the
45 * class_device is released, the coresponding kref_put is done in the release 45 * device is released, the coresponding kref_put is done in the release
46 * method. Every time one of the device's channels is allocated to a client, 46 * method. Every time one of the device's channels is allocated to a client,
47 * a kref_get occurs. When the channel is freed, the coresponding kref_put 47 * a kref_get occurs. When the channel is freed, the coresponding kref_put
48 * happens. The device's release function does a completion, so 48 * happens. The device's release function does a completion, so
49 * unregister_device does a remove event, class_device_unregister, a kref_put 49 * unregister_device does a remove event, device_unregister, a kref_put
50 * for the first reference, then waits on the completion for all other 50 * for the first reference, then waits on the completion for all other
51 * references to finish. 51 * references to finish.
52 * 52 *
@@ -77,9 +77,9 @@ static LIST_HEAD(dma_client_list);
77 77
78/* --- sysfs implementation --- */ 78/* --- sysfs implementation --- */
79 79
80static ssize_t show_memcpy_count(struct class_device *cd, char *buf) 80static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf)
81{ 81{
82 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev); 82 struct dma_chan *chan = to_dma_chan(dev);
83 unsigned long count = 0; 83 unsigned long count = 0;
84 int i; 84 int i;
85 85
@@ -89,9 +89,10 @@ static ssize_t show_memcpy_count(struct class_device *cd, char *buf)
89 return sprintf(buf, "%lu\n", count); 89 return sprintf(buf, "%lu\n", count);
90} 90}
91 91
92static ssize_t show_bytes_transferred(struct class_device *cd, char *buf) 92static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr,
93 char *buf)
93{ 94{
94 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev); 95 struct dma_chan *chan = to_dma_chan(dev);
95 unsigned long count = 0; 96 unsigned long count = 0;
96 int i; 97 int i;
97 98
@@ -101,9 +102,9 @@ static ssize_t show_bytes_transferred(struct class_device *cd, char *buf)
101 return sprintf(buf, "%lu\n", count); 102 return sprintf(buf, "%lu\n", count);
102} 103}
103 104
104static ssize_t show_in_use(struct class_device *cd, char *buf) 105static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
105{ 106{
106 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev); 107 struct dma_chan *chan = to_dma_chan(dev);
107 int in_use = 0; 108 int in_use = 0;
108 109
109 if (unlikely(chan->slow_ref) && 110 if (unlikely(chan->slow_ref) &&
@@ -119,7 +120,7 @@ static ssize_t show_in_use(struct class_device *cd, char *buf)
119 return sprintf(buf, "%d\n", in_use); 120 return sprintf(buf, "%d\n", in_use);
120} 121}
121 122
122static struct class_device_attribute dma_class_attrs[] = { 123static struct device_attribute dma_attrs[] = {
123 __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL), 124 __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
124 __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL), 125 __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
125 __ATTR(in_use, S_IRUGO, show_in_use, NULL), 126 __ATTR(in_use, S_IRUGO, show_in_use, NULL),
@@ -128,16 +129,16 @@ static struct class_device_attribute dma_class_attrs[] = {
128 129
129static void dma_async_device_cleanup(struct kref *kref); 130static void dma_async_device_cleanup(struct kref *kref);
130 131
131static void dma_class_dev_release(struct class_device *cd) 132static void dma_dev_release(struct device *dev)
132{ 133{
133 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev); 134 struct dma_chan *chan = to_dma_chan(dev);
134 kref_put(&chan->device->refcount, dma_async_device_cleanup); 135 kref_put(&chan->device->refcount, dma_async_device_cleanup);
135} 136}
136 137
137static struct class dma_devclass = { 138static struct class dma_devclass = {
138 .name = "dma", 139 .name = "dma",
139 .class_dev_attrs = dma_class_attrs, 140 .dev_attrs = dma_attrs,
140 .release = dma_class_dev_release, 141 .dev_release = dma_dev_release,
141}; 142};
142 143
143/* --- client and device registration --- */ 144/* --- client and device registration --- */
@@ -377,12 +378,12 @@ int dma_async_device_register(struct dma_device *device)
377 continue; 378 continue;
378 379
379 chan->chan_id = chancnt++; 380 chan->chan_id = chancnt++;
380 chan->class_dev.class = &dma_devclass; 381 chan->dev.class = &dma_devclass;
381 chan->class_dev.dev = NULL; 382 chan->dev.parent = NULL;
382 snprintf(chan->class_dev.class_id, BUS_ID_SIZE, "dma%dchan%d", 383 snprintf(chan->dev.bus_id, BUS_ID_SIZE, "dma%dchan%d",
383 device->dev_id, chan->chan_id); 384 device->dev_id, chan->chan_id);
384 385
385 rc = class_device_register(&chan->class_dev); 386 rc = device_register(&chan->dev);
386 if (rc) { 387 if (rc) {
387 chancnt--; 388 chancnt--;
388 free_percpu(chan->local); 389 free_percpu(chan->local);
@@ -411,7 +412,7 @@ err_out:
411 if (chan->local == NULL) 412 if (chan->local == NULL)
412 continue; 413 continue;
413 kref_put(&device->refcount, dma_async_device_cleanup); 414 kref_put(&device->refcount, dma_async_device_cleanup);
414 class_device_unregister(&chan->class_dev); 415 device_unregister(&chan->dev);
415 chancnt--; 416 chancnt--;
416 free_percpu(chan->local); 417 free_percpu(chan->local);
417 } 418 }
@@ -445,7 +446,7 @@ void dma_async_device_unregister(struct dma_device *device)
445 446
446 list_for_each_entry(chan, &device->channels, device_node) { 447 list_for_each_entry(chan, &device->channels, device_node) {
447 dma_clients_notify_removed(chan); 448 dma_clients_notify_removed(chan);
448 class_device_unregister(&chan->class_dev); 449 device_unregister(&chan->dev);
449 dma_chan_release(chan); 450 dma_chan_release(chan);
450 } 451 }
451 452
diff --git a/drivers/edac/edac_device_sysfs.c b/drivers/edac/edac_device_sysfs.c
index 70b837f23c43..53764577035f 100644
--- a/drivers/edac/edac_device_sysfs.c
+++ b/drivers/edac/edac_device_sysfs.c
@@ -246,16 +246,6 @@ int edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info *edac_dev)
246 246
247 /* Init the devices's kobject */ 247 /* Init the devices's kobject */
248 memset(&edac_dev->kobj, 0, sizeof(struct kobject)); 248 memset(&edac_dev->kobj, 0, sizeof(struct kobject));
249 edac_dev->kobj.ktype = &ktype_device_ctrl;
250
251 /* set this new device under the edac_class kobject */
252 edac_dev->kobj.parent = &edac_class->kset.kobj;
253
254 /* generate sysfs "..../edac/<name>" */
255 debugf4("%s() set name of kobject to: %s\n", __func__, edac_dev->name);
256 err = kobject_set_name(&edac_dev->kobj, "%s", edac_dev->name);
257 if (err)
258 goto err_out;
259 249
260 /* Record which module 'owns' this control structure 250 /* Record which module 'owns' this control structure
261 * and bump the ref count of the module 251 * and bump the ref count of the module
@@ -268,12 +258,15 @@ int edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info *edac_dev)
268 } 258 }
269 259
270 /* register */ 260 /* register */
271 err = kobject_register(&edac_dev->kobj); 261 err = kobject_init_and_add(&edac_dev->kobj, &ktype_device_ctrl,
262 &edac_class->kset.kobj,
263 "%s", edac_dev->name);
272 if (err) { 264 if (err) {
273 debugf1("%s()Failed to register '.../edac/%s'\n", 265 debugf1("%s()Failed to register '.../edac/%s'\n",
274 __func__, edac_dev->name); 266 __func__, edac_dev->name);
275 goto err_kobj_reg; 267 goto err_kobj_reg;
276 } 268 }
269 kobject_uevent(&edac_dev->kobj, KOBJ_ADD);
277 270
278 /* At this point, to 'free' the control struct, 271 /* At this point, to 'free' the control struct,
279 * edac_device_unregister_sysfs_main_kobj() must be used 272 * edac_device_unregister_sysfs_main_kobj() must be used
@@ -310,7 +303,7 @@ void edac_device_unregister_sysfs_main_kobj(
310 * a) module_put() this module 303 * a) module_put() this module
311 * b) 'kfree' the memory 304 * b) 'kfree' the memory
312 */ 305 */
313 kobject_unregister(&edac_dev->kobj); 306 kobject_put(&edac_dev->kobj);
314} 307}
315 308
316/* edac_dev -> instance information */ 309/* edac_dev -> instance information */
@@ -533,12 +526,6 @@ static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
533 526
534 /* init this block's kobject */ 527 /* init this block's kobject */
535 memset(&block->kobj, 0, sizeof(struct kobject)); 528 memset(&block->kobj, 0, sizeof(struct kobject));
536 block->kobj.parent = &instance->kobj;
537 block->kobj.ktype = &ktype_block_ctrl;
538
539 err = kobject_set_name(&block->kobj, "%s", block->name);
540 if (err)
541 return err;
542 529
543 /* bump the main kobject's reference count for this controller 530 /* bump the main kobject's reference count for this controller
544 * and this instance is dependant on the main 531 * and this instance is dependant on the main
@@ -550,7 +537,9 @@ static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
550 } 537 }
551 538
552 /* Add this block's kobject */ 539 /* Add this block's kobject */
553 err = kobject_register(&block->kobj); 540 err = kobject_init_and_add(&block->kobj, &ktype_block_ctrl,
541 &instance->kobj,
542 "%s", block->name);
554 if (err) { 543 if (err) {
555 debugf1("%s() Failed to register instance '%s'\n", 544 debugf1("%s() Failed to register instance '%s'\n",
556 __func__, block->name); 545 __func__, block->name);
@@ -579,12 +568,13 @@ static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
579 goto err_on_attrib; 568 goto err_on_attrib;
580 } 569 }
581 } 570 }
571 kobject_uevent(&block->kobj, KOBJ_ADD);
582 572
583 return 0; 573 return 0;
584 574
585 /* Error unwind stack */ 575 /* Error unwind stack */
586err_on_attrib: 576err_on_attrib:
587 kobject_unregister(&block->kobj); 577 kobject_put(&block->kobj);
588 578
589err_out: 579err_out:
590 return err; 580 return err;
@@ -615,7 +605,7 @@ static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
615 /* unregister this block's kobject, SEE: 605 /* unregister this block's kobject, SEE:
616 * edac_device_ctrl_block_release() callback operation 606 * edac_device_ctrl_block_release() callback operation
617 */ 607 */
618 kobject_unregister(&block->kobj); 608 kobject_put(&block->kobj);
619} 609}
620 610
621/* instance ctor/dtor code */ 611/* instance ctor/dtor code */
@@ -637,15 +627,8 @@ static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
637 /* Init the instance's kobject */ 627 /* Init the instance's kobject */
638 memset(&instance->kobj, 0, sizeof(struct kobject)); 628 memset(&instance->kobj, 0, sizeof(struct kobject));
639 629
640 /* set this new device under the edac_device main kobject */
641 instance->kobj.parent = &edac_dev->kobj;
642 instance->kobj.ktype = &ktype_instance_ctrl;
643 instance->ctl = edac_dev; 630 instance->ctl = edac_dev;
644 631
645 err = kobject_set_name(&instance->kobj, "%s", instance->name);
646 if (err)
647 goto err_out;
648
649 /* bump the main kobject's reference count for this controller 632 /* bump the main kobject's reference count for this controller
650 * and this instance is dependant on the main 633 * and this instance is dependant on the main
651 */ 634 */
@@ -655,8 +638,9 @@ static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
655 goto err_out; 638 goto err_out;
656 } 639 }
657 640
658 /* Formally register this instance's kobject */ 641 /* Formally register this instance's kobject under the edac_device */
659 err = kobject_register(&instance->kobj); 642 err = kobject_init_and_add(&instance->kobj, &ktype_instance_ctrl,
643 &edac_dev->kobj, "%s", instance->name);
660 if (err != 0) { 644 if (err != 0) {
661 debugf2("%s() Failed to register instance '%s'\n", 645 debugf2("%s() Failed to register instance '%s'\n",
662 __func__, instance->name); 646 __func__, instance->name);
@@ -679,6 +663,7 @@ static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
679 goto err_release_instance_kobj; 663 goto err_release_instance_kobj;
680 } 664 }
681 } 665 }
666 kobject_uevent(&instance->kobj, KOBJ_ADD);
682 667
683 debugf4("%s() Registered instance %d '%s' kobject\n", 668 debugf4("%s() Registered instance %d '%s' kobject\n",
684 __func__, idx, instance->name); 669 __func__, idx, instance->name);
@@ -687,7 +672,7 @@ static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
687 672
688 /* error unwind stack */ 673 /* error unwind stack */
689err_release_instance_kobj: 674err_release_instance_kobj:
690 kobject_unregister(&instance->kobj); 675 kobject_put(&instance->kobj);
691 676
692err_out: 677err_out:
693 return err; 678 return err;
@@ -712,7 +697,7 @@ static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
712 /* unregister this instance's kobject, SEE: 697 /* unregister this instance's kobject, SEE:
713 * edac_device_ctrl_instance_release() for callback operation 698 * edac_device_ctrl_instance_release() for callback operation
714 */ 699 */
715 kobject_unregister(&instance->kobj); 700 kobject_put(&instance->kobj);
716} 701}
717 702
718/* 703/*
diff --git a/drivers/edac/edac_mc_sysfs.c b/drivers/edac/edac_mc_sysfs.c
index 3706b2bc0987..9aac88027fb3 100644
--- a/drivers/edac/edac_mc_sysfs.c
+++ b/drivers/edac/edac_mc_sysfs.c
@@ -380,13 +380,6 @@ static int edac_create_csrow_object(struct mem_ctl_info *mci,
380 /* generate ..../edac/mc/mc<id>/csrow<index> */ 380 /* generate ..../edac/mc/mc<id>/csrow<index> */
381 memset(&csrow->kobj, 0, sizeof(csrow->kobj)); 381 memset(&csrow->kobj, 0, sizeof(csrow->kobj));
382 csrow->mci = mci; /* include container up link */ 382 csrow->mci = mci; /* include container up link */
383 csrow->kobj.parent = kobj_mci;
384 csrow->kobj.ktype = &ktype_csrow;
385
386 /* name this instance of csrow<id> */
387 err = kobject_set_name(&csrow->kobj, "csrow%d", index);
388 if (err)
389 goto err_out;
390 383
391 /* bump the mci instance's kobject's ref count */ 384 /* bump the mci instance's kobject's ref count */
392 kobj = kobject_get(&mci->edac_mci_kobj); 385 kobj = kobject_get(&mci->edac_mci_kobj);
@@ -396,12 +389,13 @@ static int edac_create_csrow_object(struct mem_ctl_info *mci,
396 } 389 }
397 390
398 /* Instanstiate the csrow object */ 391 /* Instanstiate the csrow object */
399 err = kobject_register(&csrow->kobj); 392 err = kobject_init_and_add(&csrow->kobj, &ktype_csrow, kobj_mci,
393 "csrow%d", index);
400 if (err) 394 if (err)
401 goto err_release_top_kobj; 395 goto err_release_top_kobj;
402 396
403 /* At this point, to release a csrow kobj, one must 397 /* At this point, to release a csrow kobj, one must
404 * call the kobject_unregister and allow that tear down 398 * call the kobject_put and allow that tear down
405 * to work the releasing 399 * to work the releasing
406 */ 400 */
407 401
@@ -412,11 +406,11 @@ static int edac_create_csrow_object(struct mem_ctl_info *mci,
412 err = edac_create_channel_files(&csrow->kobj, chan); 406 err = edac_create_channel_files(&csrow->kobj, chan);
413 if (err) { 407 if (err) {
414 /* special case the unregister here */ 408 /* special case the unregister here */
415 kobject_unregister(&csrow->kobj); 409 kobject_put(&csrow->kobj);
416 goto err_out; 410 goto err_out;
417 } 411 }
418 } 412 }
419 413 kobject_uevent(&csrow->kobj, KOBJ_ADD);
420 return 0; 414 return 0;
421 415
422 /* error unwind stack */ 416 /* error unwind stack */
@@ -744,7 +738,6 @@ static struct kobj_type ktype_mc_set_attribs = {
744 */ 738 */
745static struct kset mc_kset = { 739static struct kset mc_kset = {
746 .kobj = {.ktype = &ktype_mc_set_attribs }, 740 .kobj = {.ktype = &ktype_mc_set_attribs },
747 .ktype = &ktype_mci,
748}; 741};
749 742
750 743
@@ -765,14 +758,6 @@ int edac_mc_register_sysfs_main_kobj(struct mem_ctl_info *mci)
765 /* Init the mci's kobject */ 758 /* Init the mci's kobject */
766 memset(kobj_mci, 0, sizeof(*kobj_mci)); 759 memset(kobj_mci, 0, sizeof(*kobj_mci));
767 760
768 /* this instance become part of the mc_kset */
769 kobj_mci->kset = &mc_kset;
770
771 /* set the name of the mc<id> object */
772 err = kobject_set_name(kobj_mci, "mc%d", mci->mc_idx);
773 if (err)
774 goto fail_out;
775
776 /* Record which module 'owns' this control structure 761 /* Record which module 'owns' this control structure
777 * and bump the ref count of the module 762 * and bump the ref count of the module
778 */ 763 */
@@ -784,13 +769,18 @@ int edac_mc_register_sysfs_main_kobj(struct mem_ctl_info *mci)
784 goto fail_out; 769 goto fail_out;
785 } 770 }
786 771
772 /* this instance become part of the mc_kset */
773 kobj_mci->kset = &mc_kset;
774
787 /* register the mc<id> kobject to the mc_kset */ 775 /* register the mc<id> kobject to the mc_kset */
788 err = kobject_register(kobj_mci); 776 err = kobject_init_and_add(kobj_mci, &ktype_mci, NULL,
777 "mc%d", mci->mc_idx);
789 if (err) { 778 if (err) {
790 debugf1("%s()Failed to register '.../edac/mc%d'\n", 779 debugf1("%s()Failed to register '.../edac/mc%d'\n",
791 __func__, mci->mc_idx); 780 __func__, mci->mc_idx);
792 goto kobj_reg_fail; 781 goto kobj_reg_fail;
793 } 782 }
783 kobject_uevent(kobj_mci, KOBJ_ADD);
794 784
795 /* At this point, to 'free' the control struct, 785 /* At this point, to 'free' the control struct,
796 * edac_mc_unregister_sysfs_main_kobj() must be used 786 * edac_mc_unregister_sysfs_main_kobj() must be used
@@ -818,7 +808,7 @@ fail_out:
818void edac_mc_unregister_sysfs_main_kobj(struct mem_ctl_info *mci) 808void edac_mc_unregister_sysfs_main_kobj(struct mem_ctl_info *mci)
819{ 809{
820 /* delete the kobj from the mc_kset */ 810 /* delete the kobj from the mc_kset */
821 kobject_unregister(&mci->edac_mci_kobj); 811 kobject_put(&mci->edac_mci_kobj);
822} 812}
823 813
824#define EDAC_DEVICE_SYMLINK "device" 814#define EDAC_DEVICE_SYMLINK "device"
@@ -933,7 +923,7 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
933fail1: 923fail1:
934 for (i--; i >= 0; i--) { 924 for (i--; i >= 0; i--) {
935 if (csrow->nr_pages > 0) { 925 if (csrow->nr_pages > 0) {
936 kobject_unregister(&mci->csrows[i].kobj); 926 kobject_put(&mci->csrows[i].kobj);
937 } 927 }
938 } 928 }
939 929
@@ -960,7 +950,7 @@ void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
960 for (i = 0; i < mci->nr_csrows; i++) { 950 for (i = 0; i < mci->nr_csrows; i++) {
961 if (mci->csrows[i].nr_pages > 0) { 951 if (mci->csrows[i].nr_pages > 0) {
962 debugf0("%s() unreg csrow-%d\n", __func__, i); 952 debugf0("%s() unreg csrow-%d\n", __func__, i);
963 kobject_unregister(&mci->csrows[i].kobj); 953 kobject_put(&mci->csrows[i].kobj);
964 } 954 }
965 } 955 }
966 956
@@ -977,7 +967,7 @@ void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
977 debugf0("%s() unregister this mci kobj\n", __func__); 967 debugf0("%s() unregister this mci kobj\n", __func__);
978 968
979 /* unregister this instance's kobject */ 969 /* unregister this instance's kobject */
980 kobject_unregister(&mci->edac_mci_kobj); 970 kobject_put(&mci->edac_mci_kobj);
981} 971}
982 972
983 973
diff --git a/drivers/edac/edac_module.c b/drivers/edac/edac_module.c
index e0c4a4086055..7e1374afd967 100644
--- a/drivers/edac/edac_module.c
+++ b/drivers/edac/edac_module.c
@@ -31,7 +31,7 @@ struct workqueue_struct *edac_workqueue;
31 * need to export to other files in this modules 31 * need to export to other files in this modules
32 */ 32 */
33static struct sysdev_class edac_class = { 33static struct sysdev_class edac_class = {
34 set_kset_name("edac"), 34 .name = "edac",
35}; 35};
36static int edac_class_valid; 36static int edac_class_valid;
37 37
diff --git a/drivers/edac/edac_pci_sysfs.c b/drivers/edac/edac_pci_sysfs.c
index 69f5dddabddf..5b075da99145 100644
--- a/drivers/edac/edac_pci_sysfs.c
+++ b/drivers/edac/edac_pci_sysfs.c
@@ -162,14 +162,6 @@ static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
162 162
163 debugf0("%s()\n", __func__); 163 debugf0("%s()\n", __func__);
164 164
165 /* Set the parent and the instance's ktype */
166 pci->kobj.parent = &edac_pci_top_main_kobj;
167 pci->kobj.ktype = &ktype_pci_instance;
168
169 err = kobject_set_name(&pci->kobj, "pci%d", idx);
170 if (err)
171 return err;
172
173 /* First bump the ref count on the top main kobj, which will 165 /* First bump the ref count on the top main kobj, which will
174 * track the number of PCI instances we have, and thus nest 166 * track the number of PCI instances we have, and thus nest
175 * properly on keeping the module loaded 167 * properly on keeping the module loaded
@@ -181,7 +173,8 @@ static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
181 } 173 }
182 174
183 /* And now register this new kobject under the main kobj */ 175 /* And now register this new kobject under the main kobj */
184 err = kobject_register(&pci->kobj); 176 err = kobject_init_and_add(&pci->kobj, &ktype_pci_instance,
177 &edac_pci_top_main_kobj, "pci%d", idx);
185 if (err != 0) { 178 if (err != 0) {
186 debugf2("%s() failed to register instance pci%d\n", 179 debugf2("%s() failed to register instance pci%d\n",
187 __func__, idx); 180 __func__, idx);
@@ -189,6 +182,7 @@ static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
189 goto error_out; 182 goto error_out;
190 } 183 }
191 184
185 kobject_uevent(&pci->kobj, KOBJ_ADD);
192 debugf1("%s() Register instance 'pci%d' kobject\n", __func__, idx); 186 debugf1("%s() Register instance 'pci%d' kobject\n", __func__, idx);
193 187
194 return 0; 188 return 0;
@@ -211,7 +205,7 @@ void edac_pci_unregister_sysfs_instance_kobj(struct edac_pci_ctl_info *pci)
211 * function release the main reference count and then 205 * function release the main reference count and then
212 * kfree the memory 206 * kfree the memory
213 */ 207 */
214 kobject_unregister(&pci->kobj); 208 kobject_put(&pci->kobj);
215} 209}
216 210
217/***************************** EDAC PCI sysfs root **********************/ 211/***************************** EDAC PCI sysfs root **********************/
@@ -364,14 +358,6 @@ int edac_pci_main_kobj_setup(void)
364 goto decrement_count_fail; 358 goto decrement_count_fail;
365 } 359 }
366 360
367 /* Need the kobject hook ups, and name setting */
368 edac_pci_top_main_kobj.ktype = &ktype_edac_pci_main_kobj;
369 edac_pci_top_main_kobj.parent = &edac_class->kset.kobj;
370
371 err = kobject_set_name(&edac_pci_top_main_kobj, "pci");
372 if (err)
373 goto decrement_count_fail;
374
375 /* Bump the reference count on this module to ensure the 361 /* Bump the reference count on this module to ensure the
376 * modules isn't unloaded until we deconstruct the top 362 * modules isn't unloaded until we deconstruct the top
377 * level main kobj for EDAC PCI 363 * level main kobj for EDAC PCI
@@ -383,23 +369,24 @@ int edac_pci_main_kobj_setup(void)
383 } 369 }
384 370
385 /* Instanstiate the pci object */ 371 /* Instanstiate the pci object */
386 /* FIXME: maybe new sysdev_create_subdir() */ 372 err = kobject_init_and_add(&edac_pci_top_main_kobj, &ktype_edac_pci_main_kobj,
387 err = kobject_register(&edac_pci_top_main_kobj); 373 &edac_class->kset.kobj, "pci");
388 if (err) { 374 if (err) {
389 debugf1("Failed to register '.../edac/pci'\n"); 375 debugf1("Failed to register '.../edac/pci'\n");
390 goto kobject_register_fail; 376 goto kobject_init_and_add_fail;
391 } 377 }
392 378
393 /* At this point, to 'release' the top level kobject 379 /* At this point, to 'release' the top level kobject
394 * for EDAC PCI, then edac_pci_main_kobj_teardown() 380 * for EDAC PCI, then edac_pci_main_kobj_teardown()
395 * must be used, for resources to be cleaned up properly 381 * must be used, for resources to be cleaned up properly
396 */ 382 */
383 kobject_uevent(&edac_pci_top_main_kobj, KOBJ_ADD);
397 debugf1("Registered '.../edac/pci' kobject\n"); 384 debugf1("Registered '.../edac/pci' kobject\n");
398 385
399 return 0; 386 return 0;
400 387
401 /* Error unwind statck */ 388 /* Error unwind statck */
402kobject_register_fail: 389kobject_init_and_add_fail:
403 module_put(THIS_MODULE); 390 module_put(THIS_MODULE);
404 391
405decrement_count_fail: 392decrement_count_fail:
@@ -424,9 +411,9 @@ static void edac_pci_main_kobj_teardown(void)
424 * main kobj 411 * main kobj
425 */ 412 */
426 if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0) { 413 if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0) {
427 debugf0("%s() called kobject_unregister on main kobj\n", 414 debugf0("%s() called kobject_put on main kobj\n",
428 __func__); 415 __func__);
429 kobject_unregister(&edac_pci_top_main_kobj); 416 kobject_put(&edac_pci_top_main_kobj);
430 } 417 }
431} 418}
432 419
diff --git a/drivers/firmware/edd.c b/drivers/firmware/edd.c
index 6942e065e609..d168223db159 100644
--- a/drivers/firmware/edd.c
+++ b/drivers/firmware/edd.c
@@ -631,7 +631,7 @@ static struct kobj_type edd_ktype = {
631 .default_attrs = def_attrs, 631 .default_attrs = def_attrs,
632}; 632};
633 633
634static decl_subsys(edd, &edd_ktype, NULL); 634static struct kset *edd_kset;
635 635
636 636
637/** 637/**
@@ -693,7 +693,7 @@ edd_create_symlink_to_pcidev(struct edd_device *edev)
693static inline void 693static inline void
694edd_device_unregister(struct edd_device *edev) 694edd_device_unregister(struct edd_device *edev)
695{ 695{
696 kobject_unregister(&edev->kobj); 696 kobject_put(&edev->kobj);
697} 697}
698 698
699static void edd_populate_dir(struct edd_device * edev) 699static void edd_populate_dir(struct edd_device * edev)
@@ -721,12 +721,13 @@ edd_device_register(struct edd_device *edev, int i)
721 if (!edev) 721 if (!edev)
722 return 1; 722 return 1;
723 edd_dev_set_info(edev, i); 723 edd_dev_set_info(edev, i);
724 kobject_set_name(&edev->kobj, "int13_dev%02x", 724 edev->kobj.kset = edd_kset;
725 0x80 + i); 725 error = kobject_init_and_add(&edev->kobj, &edd_ktype, NULL,
726 kobj_set_kset_s(edev,edd_subsys); 726 "int13_dev%02x", 0x80 + i);
727 error = kobject_register(&edev->kobj); 727 if (!error) {
728 if (!error)
729 edd_populate_dir(edev); 728 edd_populate_dir(edev);
729 kobject_uevent(&edev->kobj, KOBJ_ADD);
730 }
730 return error; 731 return error;
731} 732}
732 733
@@ -755,9 +756,9 @@ edd_init(void)
755 return 1; 756 return 1;
756 } 757 }
757 758
758 rc = firmware_register(&edd_subsys); 759 edd_kset = kset_create_and_add("edd", NULL, firmware_kobj);
759 if (rc) 760 if (!edd_kset)
760 return rc; 761 return -ENOMEM;
761 762
762 for (i = 0; i < edd_num_devices() && !rc; i++) { 763 for (i = 0; i < edd_num_devices() && !rc; i++) {
763 edev = kzalloc(sizeof (*edev), GFP_KERNEL); 764 edev = kzalloc(sizeof (*edev), GFP_KERNEL);
@@ -773,7 +774,7 @@ edd_init(void)
773 } 774 }
774 775
775 if (rc) 776 if (rc)
776 firmware_unregister(&edd_subsys); 777 kset_unregister(edd_kset);
777 return rc; 778 return rc;
778} 779}
779 780
@@ -787,7 +788,7 @@ edd_exit(void)
787 if ((edev = edd_devices[i])) 788 if ((edev = edd_devices[i]))
788 edd_device_unregister(edev); 789 edd_device_unregister(edev);
789 } 790 }
790 firmware_unregister(&edd_subsys); 791 kset_unregister(edd_kset);
791} 792}
792 793
793late_initcall(edd_init); 794late_initcall(edd_init);
diff --git a/drivers/firmware/efivars.c b/drivers/firmware/efivars.c
index 858a7b95933b..f4f709d1370b 100644
--- a/drivers/firmware/efivars.c
+++ b/drivers/firmware/efivars.c
@@ -129,13 +129,6 @@ struct efivar_attribute {
129}; 129};
130 130
131 131
132#define EFI_ATTR(_name, _mode, _show, _store) \
133struct subsys_attribute efi_attr_##_name = { \
134 .attr = {.name = __stringify(_name), .mode = _mode}, \
135 .show = _show, \
136 .store = _store, \
137};
138
139#define EFIVAR_ATTR(_name, _mode, _show, _store) \ 132#define EFIVAR_ATTR(_name, _mode, _show, _store) \
140struct efivar_attribute efivar_attr_##_name = { \ 133struct efivar_attribute efivar_attr_##_name = { \
141 .attr = {.name = __stringify(_name), .mode = _mode}, \ 134 .attr = {.name = __stringify(_name), .mode = _mode}, \
@@ -143,13 +136,6 @@ struct efivar_attribute efivar_attr_##_name = { \
143 .store = _store, \ 136 .store = _store, \
144}; 137};
145 138
146#define VAR_SUBSYS_ATTR(_name, _mode, _show, _store) \
147struct subsys_attribute var_subsys_attr_##_name = { \
148 .attr = {.name = __stringify(_name), .mode = _mode}, \
149 .show = _show, \
150 .store = _store, \
151};
152
153#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr) 139#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
154#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj) 140#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
155 141
@@ -408,21 +394,16 @@ static struct kobj_type efivar_ktype = {
408 .default_attrs = def_attrs, 394 .default_attrs = def_attrs,
409}; 395};
410 396
411static ssize_t
412dummy(struct kset *kset, char *buf)
413{
414 return -ENODEV;
415}
416
417static inline void 397static inline void
418efivar_unregister(struct efivar_entry *var) 398efivar_unregister(struct efivar_entry *var)
419{ 399{
420 kobject_unregister(&var->kobj); 400 kobject_put(&var->kobj);
421} 401}
422 402
423 403
424static ssize_t 404static ssize_t efivar_create(struct kobject *kobj,
425efivar_create(struct kset *kset, const char *buf, size_t count) 405 struct bin_attribute *bin_attr,
406 char *buf, loff_t pos, size_t count)
426{ 407{
427 struct efi_variable *new_var = (struct efi_variable *)buf; 408 struct efi_variable *new_var = (struct efi_variable *)buf;
428 struct efivar_entry *search_efivar, *n; 409 struct efivar_entry *search_efivar, *n;
@@ -479,8 +460,9 @@ efivar_create(struct kset *kset, const char *buf, size_t count)
479 return count; 460 return count;
480} 461}
481 462
482static ssize_t 463static ssize_t efivar_delete(struct kobject *kobj,
483efivar_delete(struct kset *kset, const char *buf, size_t count) 464 struct bin_attribute *bin_attr,
465 char *buf, loff_t pos, size_t count)
484{ 466{
485 struct efi_variable *del_var = (struct efi_variable *)buf; 467 struct efi_variable *del_var = (struct efi_variable *)buf;
486 struct efivar_entry *search_efivar, *n; 468 struct efivar_entry *search_efivar, *n;
@@ -537,25 +519,26 @@ efivar_delete(struct kset *kset, const char *buf, size_t count)
537 return count; 519 return count;
538} 520}
539 521
540static VAR_SUBSYS_ATTR(new_var, 0200, dummy, efivar_create); 522static struct bin_attribute var_subsys_attr_new_var = {
541static VAR_SUBSYS_ATTR(del_var, 0200, dummy, efivar_delete); 523 .attr = {.name = "new_var", .mode = 0200},
524 .write = efivar_create,
525};
542 526
543static struct subsys_attribute *var_subsys_attrs[] = { 527static struct bin_attribute var_subsys_attr_del_var = {
544 &var_subsys_attr_new_var, 528 .attr = {.name = "del_var", .mode = 0200},
545 &var_subsys_attr_del_var, 529 .write = efivar_delete,
546 NULL,
547}; 530};
548 531
549/* 532/*
550 * Let's not leave out systab information that snuck into 533 * Let's not leave out systab information that snuck into
551 * the efivars driver 534 * the efivars driver
552 */ 535 */
553static ssize_t 536static ssize_t systab_show(struct kobject *kobj,
554systab_read(struct kset *kset, char *buf) 537 struct kobj_attribute *attr, char *buf)
555{ 538{
556 char *str = buf; 539 char *str = buf;
557 540
558 if (!kset || !buf) 541 if (!kobj || !buf)
559 return -EINVAL; 542 return -EINVAL;
560 543
561 if (efi.mps != EFI_INVALID_TABLE_ADDR) 544 if (efi.mps != EFI_INVALID_TABLE_ADDR)
@@ -576,15 +559,21 @@ systab_read(struct kset *kset, char *buf)
576 return str - buf; 559 return str - buf;
577} 560}
578 561
579static EFI_ATTR(systab, 0400, systab_read, NULL); 562static struct kobj_attribute efi_attr_systab =
563 __ATTR(systab, 0400, systab_show, NULL);
580 564
581static struct subsys_attribute *efi_subsys_attrs[] = { 565static struct attribute *efi_subsys_attrs[] = {
582 &efi_attr_systab, 566 &efi_attr_systab.attr,
583 NULL, /* maybe more in the future? */ 567 NULL, /* maybe more in the future? */
584}; 568};
585 569
586static decl_subsys(vars, &efivar_ktype, NULL); 570static struct attribute_group efi_subsys_attr_group = {
587static decl_subsys(efi, NULL, NULL); 571 .attrs = efi_subsys_attrs,
572};
573
574
575static struct kset *vars_kset;
576static struct kobject *efi_kobj;
588 577
589/* 578/*
590 * efivar_create_sysfs_entry() 579 * efivar_create_sysfs_entry()
@@ -628,15 +617,16 @@ efivar_create_sysfs_entry(unsigned long variable_name_size,
628 *(short_name + strlen(short_name)) = '-'; 617 *(short_name + strlen(short_name)) = '-';
629 efi_guid_unparse(vendor_guid, short_name + strlen(short_name)); 618 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
630 619
631 kobject_set_name(&new_efivar->kobj, "%s", short_name); 620 new_efivar->kobj.kset = vars_kset;
632 kobj_set_kset_s(new_efivar, vars_subsys); 621 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
633 i = kobject_register(&new_efivar->kobj); 622 "%s", short_name);
634 if (i) { 623 if (i) {
635 kfree(short_name); 624 kfree(short_name);
636 kfree(new_efivar); 625 kfree(new_efivar);
637 return 1; 626 return 1;
638 } 627 }
639 628
629 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
640 kfree(short_name); 630 kfree(short_name);
641 short_name = NULL; 631 short_name = NULL;
642 632
@@ -660,9 +650,8 @@ efivars_init(void)
660 efi_status_t status = EFI_NOT_FOUND; 650 efi_status_t status = EFI_NOT_FOUND;
661 efi_guid_t vendor_guid; 651 efi_guid_t vendor_guid;
662 efi_char16_t *variable_name; 652 efi_char16_t *variable_name;
663 struct subsys_attribute *attr;
664 unsigned long variable_name_size = 1024; 653 unsigned long variable_name_size = 1024;
665 int i, error = 0; 654 int error = 0;
666 655
667 if (!efi_enabled) 656 if (!efi_enabled)
668 return -ENODEV; 657 return -ENODEV;
@@ -676,23 +665,18 @@ efivars_init(void)
676 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION, 665 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
677 EFIVARS_DATE); 666 EFIVARS_DATE);
678 667
679 /* 668 /* For now we'll register the efi directory at /sys/firmware/efi */
680 * For now we'll register the efi subsys within this driver 669 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
681 */ 670 if (!efi_kobj) {
682 671 printk(KERN_ERR "efivars: Firmware registration failed.\n");
683 error = firmware_register(&efi_subsys); 672 error = -ENOMEM;
684
685 if (error) {
686 printk(KERN_ERR "efivars: Firmware registration failed with error %d.\n", error);
687 goto out_free; 673 goto out_free;
688 } 674 }
689 675
690 kobj_set_kset_s(&vars_subsys, efi_subsys); 676 vars_kset = kset_create_and_add("vars", NULL, efi_kobj);
691 677 if (!vars_kset) {
692 error = subsystem_register(&vars_subsys); 678 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
693 679 error = -ENOMEM;
694 if (error) {
695 printk(KERN_ERR "efivars: Subsystem registration failed with error %d.\n", error);
696 goto out_firmware_unregister; 680 goto out_firmware_unregister;
697 } 681 }
698 682
@@ -727,28 +711,28 @@ efivars_init(void)
727 * Now add attributes to allow creation of new vars 711 * Now add attributes to allow creation of new vars
728 * and deletion of existing ones... 712 * and deletion of existing ones...
729 */ 713 */
730 714 error = sysfs_create_bin_file(&vars_kset->kobj,
731 for (i = 0; (attr = var_subsys_attrs[i]) && !error; i++) { 715 &var_subsys_attr_new_var);
732 if (attr->show && attr->store) 716 if (error)
733 error = subsys_create_file(&vars_subsys, attr); 717 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
734 } 718 " due to error %d\n", error);
719 error = sysfs_create_bin_file(&vars_kset->kobj,
720 &var_subsys_attr_del_var);
721 if (error)
722 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
723 " due to error %d\n", error);
735 724
736 /* Don't forget the systab entry */ 725 /* Don't forget the systab entry */
737 726 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
738 for (i = 0; (attr = efi_subsys_attrs[i]) && !error; i++) {
739 if (attr->show)
740 error = subsys_create_file(&efi_subsys, attr);
741 }
742
743 if (error) 727 if (error)
744 printk(KERN_ERR "efivars: Sysfs attribute export failed with error %d.\n", error); 728 printk(KERN_ERR "efivars: Sysfs attribute export failed with error %d.\n", error);
745 else 729 else
746 goto out_free; 730 goto out_free;
747 731
748 subsystem_unregister(&vars_subsys); 732 kset_unregister(vars_kset);
749 733
750out_firmware_unregister: 734out_firmware_unregister:
751 firmware_unregister(&efi_subsys); 735 kobject_put(efi_kobj);
752 736
753out_free: 737out_free:
754 kfree(variable_name); 738 kfree(variable_name);
@@ -768,8 +752,8 @@ efivars_exit(void)
768 efivar_unregister(entry); 752 efivar_unregister(entry);
769 } 753 }
770 754
771 subsystem_unregister(&vars_subsys); 755 kset_unregister(vars_kset);
772 firmware_unregister(&efi_subsys); 756 kobject_put(efi_kobj);
773} 757}
774 758
775module_init(efivars_init); 759module_init(efivars_init);
diff --git a/drivers/i2c/chips/isp1301_omap.c b/drivers/i2c/chips/isp1301_omap.c
index b767603a07ba..ebfbb2947ae6 100644
--- a/drivers/i2c/chips/isp1301_omap.c
+++ b/drivers/i2c/chips/isp1301_omap.c
@@ -259,12 +259,6 @@ static inline const char *state_name(struct isp1301 *isp)
259 return state_string(isp->otg.state); 259 return state_string(isp->otg.state);
260} 260}
261 261
262#ifdef VERBOSE
263#define dev_vdbg dev_dbg
264#else
265#define dev_vdbg(dev, fmt, arg...) do{}while(0)
266#endif
267
268/*-------------------------------------------------------------------------*/ 262/*-------------------------------------------------------------------------*/
269 263
270/* NOTE: some of this ISP1301 setup is specific to H2 boards; 264/* NOTE: some of this ISP1301 setup is specific to H2 boards;
diff --git a/drivers/ide/ide-probe.c b/drivers/ide/ide-probe.c
index 2994523be7bf..0cb3d2bb3ab9 100644
--- a/drivers/ide/ide-probe.c
+++ b/drivers/ide/ide-probe.c
@@ -1173,7 +1173,7 @@ static struct kobject *exact_match(dev_t dev, int *part, void *data)
1173{ 1173{
1174 struct gendisk *p = data; 1174 struct gendisk *p = data;
1175 *part &= (1 << PARTN_BITS) - 1; 1175 *part &= (1 << PARTN_BITS) - 1;
1176 return &p->kobj; 1176 return &p->dev.kobj;
1177} 1177}
1178 1178
1179static int exact_lock(dev_t dev, void *data) 1179static int exact_lock(dev_t dev, void *data)
diff --git a/drivers/ide/ide-tape.c b/drivers/ide/ide-tape.c
index 7b9181b5469d..1495792d7917 100644
--- a/drivers/ide/ide-tape.c
+++ b/drivers/ide/ide-tape.c
@@ -4724,10 +4724,8 @@ static void ide_tape_release(struct kref *kref)
4724 4724
4725 drive->dsc_overlap = 0; 4725 drive->dsc_overlap = 0;
4726 drive->driver_data = NULL; 4726 drive->driver_data = NULL;
4727 class_device_destroy(idetape_sysfs_class, 4727 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
4728 MKDEV(IDETAPE_MAJOR, tape->minor)); 4728 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
4729 class_device_destroy(idetape_sysfs_class,
4730 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
4731 idetape_devs[tape->minor] = NULL; 4729 idetape_devs[tape->minor] = NULL;
4732 g->private_data = NULL; 4730 g->private_data = NULL;
4733 put_disk(g); 4731 put_disk(g);
@@ -4884,10 +4882,10 @@ static int ide_tape_probe(ide_drive_t *drive)
4884 4882
4885 idetape_setup(drive, tape, minor); 4883 idetape_setup(drive, tape, minor);
4886 4884
4887 class_device_create(idetape_sysfs_class, NULL, 4885 device_create(idetape_sysfs_class, &drive->gendev,
4888 MKDEV(IDETAPE_MAJOR, minor), &drive->gendev, "%s", tape->name); 4886 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4889 class_device_create(idetape_sysfs_class, NULL, 4887 device_create(idetape_sysfs_class, &drive->gendev,
4890 MKDEV(IDETAPE_MAJOR, minor + 128), &drive->gendev, "n%s", tape->name); 4888 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
4891 4889
4892 g->fops = &idetape_block_ops; 4890 g->fops = &idetape_block_ops;
4893 ide_register_region(g); 4891 ide_register_region(g);
diff --git a/drivers/ieee1394/nodemgr.c b/drivers/ieee1394/nodemgr.c
index 90dc75be3418..511e4321c6b6 100644
--- a/drivers/ieee1394/nodemgr.c
+++ b/drivers/ieee1394/nodemgr.c
@@ -727,33 +727,31 @@ static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
727 727
728static DEFINE_MUTEX(nodemgr_serialize_remove_uds); 728static DEFINE_MUTEX(nodemgr_serialize_remove_uds);
729 729
730static int __match_ne(struct device *dev, void *data)
731{
732 struct unit_directory *ud;
733 struct node_entry *ne = (struct node_entry *)data;
734
735 ud = container_of(dev, struct unit_directory, unit_dev);
736 return ud->ne == ne;
737}
738
730static void nodemgr_remove_uds(struct node_entry *ne) 739static void nodemgr_remove_uds(struct node_entry *ne)
731{ 740{
732 struct device *dev; 741 struct device *dev;
733 struct unit_directory *tmp, *ud; 742 struct unit_directory *ud;
734 743
735 /* Iteration over nodemgr_ud_class.devices has to be protected by 744 /* Use class_find device to iterate the devices. Since this code
736 * nodemgr_ud_class.sem, but device_unregister() will eventually 745 * may be called from other contexts besides the knodemgrds,
737 * take nodemgr_ud_class.sem too. Therefore pick out one ud at a time, 746 * protect it by nodemgr_serialize_remove_uds.
738 * release the semaphore, and then unregister the ud. Since this code
739 * may be called from other contexts besides the knodemgrds, protect the
740 * gap after release of the semaphore by nodemgr_serialize_remove_uds.
741 */ 747 */
742 mutex_lock(&nodemgr_serialize_remove_uds); 748 mutex_lock(&nodemgr_serialize_remove_uds);
743 for (;;) { 749 for (;;) {
744 ud = NULL; 750 dev = class_find_device(&nodemgr_ud_class, ne, __match_ne);
745 down(&nodemgr_ud_class.sem); 751 if (!dev)
746 list_for_each_entry(dev, &nodemgr_ud_class.devices, node) {
747 tmp = container_of(dev, struct unit_directory,
748 unit_dev);
749 if (tmp->ne == ne) {
750 ud = tmp;
751 break;
752 }
753 }
754 up(&nodemgr_ud_class.sem);
755 if (ud == NULL)
756 break; 752 break;
753 ud = container_of(dev, struct unit_directory, unit_dev);
754 put_device(dev);
757 device_unregister(&ud->unit_dev); 755 device_unregister(&ud->unit_dev);
758 device_unregister(&ud->device); 756 device_unregister(&ud->device);
759 } 757 }
@@ -882,45 +880,66 @@ fail_alloc:
882 return NULL; 880 return NULL;
883} 881}
884 882
883static int __match_ne_guid(struct device *dev, void *data)
884{
885 struct node_entry *ne;
886 u64 *guid = (u64 *)data;
887
888 ne = container_of(dev, struct node_entry, node_dev);
889 return ne->guid == *guid;
890}
885 891
886static struct node_entry *find_entry_by_guid(u64 guid) 892static struct node_entry *find_entry_by_guid(u64 guid)
887{ 893{
888 struct device *dev; 894 struct device *dev;
889 struct node_entry *ne, *ret_ne = NULL; 895 struct node_entry *ne;
890
891 down(&nodemgr_ne_class.sem);
892 list_for_each_entry(dev, &nodemgr_ne_class.devices, node) {
893 ne = container_of(dev, struct node_entry, node_dev);
894 896
895 if (ne->guid == guid) { 897 dev = class_find_device(&nodemgr_ne_class, &guid, __match_ne_guid);
896 ret_ne = ne; 898 if (!dev)
897 break; 899 return NULL;
898 } 900 ne = container_of(dev, struct node_entry, node_dev);
899 } 901 put_device(dev);
900 up(&nodemgr_ne_class.sem);
901 902
902 return ret_ne; 903 return ne;
903} 904}
904 905
906struct match_nodeid_param {
907 struct hpsb_host *host;
908 nodeid_t nodeid;
909};
910
911static int __match_ne_nodeid(struct device *dev, void *data)
912{
913 int found = 0;
914 struct node_entry *ne;
915 struct match_nodeid_param *param = (struct match_nodeid_param *)data;
916
917 if (!dev)
918 goto ret;
919 ne = container_of(dev, struct node_entry, node_dev);
920 if (ne->host == param->host && ne->nodeid == param->nodeid)
921 found = 1;
922ret:
923 return found;
924}
905 925
906static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, 926static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
907 nodeid_t nodeid) 927 nodeid_t nodeid)
908{ 928{
909 struct device *dev; 929 struct device *dev;
910 struct node_entry *ne, *ret_ne = NULL; 930 struct node_entry *ne;
931 struct match_nodeid_param param;
911 932
912 down(&nodemgr_ne_class.sem); 933 param.host = host;
913 list_for_each_entry(dev, &nodemgr_ne_class.devices, node) { 934 param.nodeid = nodeid;
914 ne = container_of(dev, struct node_entry, node_dev);
915 935
916 if (ne->host == host && ne->nodeid == nodeid) { 936 dev = class_find_device(&nodemgr_ne_class, &param, __match_ne_nodeid);
917 ret_ne = ne; 937 if (!dev)
918 break; 938 return NULL;
919 } 939 ne = container_of(dev, struct node_entry, node_dev);
920 } 940 put_device(dev);
921 up(&nodemgr_ne_class.sem);
922 941
923 return ret_ne; 942 return ne;
924} 943}
925 944
926 945
@@ -1370,107 +1389,109 @@ static void nodemgr_node_scan(struct host_info *hi, int generation)
1370 } 1389 }
1371} 1390}
1372 1391
1373 1392static int __nodemgr_driver_suspend(struct device *dev, void *data)
1374static void nodemgr_suspend_ne(struct node_entry *ne)
1375{ 1393{
1376 struct device *dev;
1377 struct unit_directory *ud; 1394 struct unit_directory *ud;
1378 struct device_driver *drv; 1395 struct device_driver *drv;
1396 struct node_entry *ne = (struct node_entry *)data;
1379 int error; 1397 int error;
1380 1398
1381 HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]", 1399 ud = container_of(dev, struct unit_directory, unit_dev);
1382 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid); 1400 if (ud->ne == ne) {
1401 drv = get_driver(ud->device.driver);
1402 if (drv) {
1403 error = 1; /* release if suspend is not implemented */
1404 if (drv->suspend) {
1405 down(&ud->device.sem);
1406 error = drv->suspend(&ud->device, PMSG_SUSPEND);
1407 up(&ud->device.sem);
1408 }
1409 if (error)
1410 device_release_driver(&ud->device);
1411 put_driver(drv);
1412 }
1413 }
1383 1414
1384 ne->in_limbo = 1; 1415 return 0;
1385 WARN_ON(device_create_file(&ne->device, &dev_attr_ne_in_limbo)); 1416}
1386 1417
1387 down(&nodemgr_ud_class.sem); 1418static int __nodemgr_driver_resume(struct device *dev, void *data)
1388 list_for_each_entry(dev, &nodemgr_ud_class.devices, node) { 1419{
1389 ud = container_of(dev, struct unit_directory, unit_dev); 1420 struct unit_directory *ud;
1390 if (ud->ne != ne) 1421 struct device_driver *drv;
1391 continue; 1422 struct node_entry *ne = (struct node_entry *)data;
1392 1423
1424 ud = container_of(dev, struct unit_directory, unit_dev);
1425 if (ud->ne == ne) {
1393 drv = get_driver(ud->device.driver); 1426 drv = get_driver(ud->device.driver);
1394 if (!drv) 1427 if (drv) {
1395 continue; 1428 if (drv->resume) {
1396 1429 down(&ud->device.sem);
1397 error = 1; /* release if suspend is not implemented */ 1430 drv->resume(&ud->device);
1398 if (drv->suspend) { 1431 up(&ud->device.sem);
1399 down(&ud->device.sem); 1432 }
1400 error = drv->suspend(&ud->device, PMSG_SUSPEND); 1433 put_driver(drv);
1401 up(&ud->device.sem);
1402 } 1434 }
1403 if (error)
1404 device_release_driver(&ud->device);
1405 put_driver(drv);
1406 } 1435 }
1407 up(&nodemgr_ud_class.sem);
1408}
1409 1436
1437 return 0;
1438}
1410 1439
1411static void nodemgr_resume_ne(struct node_entry *ne) 1440static void nodemgr_suspend_ne(struct node_entry *ne)
1412{ 1441{
1413 struct device *dev; 1442 HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1414 struct unit_directory *ud; 1443 NODE_BUS_ARGS(ne->host, ne->nodeid),
1415 struct device_driver *drv; 1444 (unsigned long long)ne->guid);
1416 1445
1417 ne->in_limbo = 0; 1446 ne->in_limbo = 1;
1418 device_remove_file(&ne->device, &dev_attr_ne_in_limbo); 1447 WARN_ON(device_create_file(&ne->device, &dev_attr_ne_in_limbo));
1419 1448
1420 down(&nodemgr_ud_class.sem); 1449 class_for_each_device(&nodemgr_ud_class, ne, __nodemgr_driver_suspend);
1421 list_for_each_entry(dev, &nodemgr_ud_class.devices, node) { 1450}
1422 ud = container_of(dev, struct unit_directory, unit_dev);
1423 if (ud->ne != ne)
1424 continue;
1425 1451
1426 drv = get_driver(ud->device.driver);
1427 if (!drv)
1428 continue;
1429 1452
1430 if (drv->resume) { 1453static void nodemgr_resume_ne(struct node_entry *ne)
1431 down(&ud->device.sem); 1454{
1432 drv->resume(&ud->device); 1455 ne->in_limbo = 0;
1433 up(&ud->device.sem); 1456 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1434 }
1435 put_driver(drv);
1436 }
1437 up(&nodemgr_ud_class.sem);
1438 1457
1458 class_for_each_device(&nodemgr_ud_class, ne, __nodemgr_driver_resume);
1439 HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]", 1459 HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1440 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid); 1460 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1441} 1461}
1442 1462
1443 1463static int __nodemgr_update_pdrv(struct device *dev, void *data)
1444static void nodemgr_update_pdrv(struct node_entry *ne)
1445{ 1464{
1446 struct device *dev;
1447 struct unit_directory *ud; 1465 struct unit_directory *ud;
1448 struct device_driver *drv; 1466 struct device_driver *drv;
1449 struct hpsb_protocol_driver *pdrv; 1467 struct hpsb_protocol_driver *pdrv;
1468 struct node_entry *ne = (struct node_entry *)data;
1450 int error; 1469 int error;
1451 1470
1452 down(&nodemgr_ud_class.sem); 1471 ud = container_of(dev, struct unit_directory, unit_dev);
1453 list_for_each_entry(dev, &nodemgr_ud_class.devices, node) { 1472 if (ud->ne == ne) {
1454 ud = container_of(dev, struct unit_directory, unit_dev);
1455 if (ud->ne != ne)
1456 continue;
1457
1458 drv = get_driver(ud->device.driver); 1473 drv = get_driver(ud->device.driver);
1459 if (!drv) 1474 if (drv) {
1460 continue; 1475 error = 0;
1461 1476 pdrv = container_of(drv, struct hpsb_protocol_driver,
1462 error = 0; 1477 driver);
1463 pdrv = container_of(drv, struct hpsb_protocol_driver, driver); 1478 if (pdrv->update) {
1464 if (pdrv->update) { 1479 down(&ud->device.sem);
1465 down(&ud->device.sem); 1480 error = pdrv->update(ud);
1466 error = pdrv->update(ud); 1481 up(&ud->device.sem);
1467 up(&ud->device.sem); 1482 }
1483 if (error)
1484 device_release_driver(&ud->device);
1485 put_driver(drv);
1468 } 1486 }
1469 if (error)
1470 device_release_driver(&ud->device);
1471 put_driver(drv);
1472 } 1487 }
1473 up(&nodemgr_ud_class.sem); 1488
1489 return 0;
1490}
1491
1492static void nodemgr_update_pdrv(struct node_entry *ne)
1493{
1494 class_for_each_device(&nodemgr_ud_class, ne, __nodemgr_update_pdrv);
1474} 1495}
1475 1496
1476 1497
@@ -1529,13 +1550,31 @@ static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int ge
1529 put_device(dev); 1550 put_device(dev);
1530} 1551}
1531 1552
1553struct probe_param {
1554 struct host_info *hi;
1555 int generation;
1556};
1557
1558static int __nodemgr_node_probe(struct device *dev, void *data)
1559{
1560 struct probe_param *param = (struct probe_param *)data;
1561 struct node_entry *ne;
1562
1563 ne = container_of(dev, struct node_entry, node_dev);
1564 if (!ne->needs_probe)
1565 nodemgr_probe_ne(param->hi, ne, param->generation);
1566 if (ne->needs_probe)
1567 nodemgr_probe_ne(param->hi, ne, param->generation);
1568 return 0;
1569}
1532 1570
1533static void nodemgr_node_probe(struct host_info *hi, int generation) 1571static void nodemgr_node_probe(struct host_info *hi, int generation)
1534{ 1572{
1535 struct hpsb_host *host = hi->host; 1573 struct hpsb_host *host = hi->host;
1536 struct device *dev; 1574 struct probe_param param;
1537 struct node_entry *ne;
1538 1575
1576 param.hi = hi;
1577 param.generation = generation;
1539 /* Do some processing of the nodes we've probed. This pulls them 1578 /* Do some processing of the nodes we've probed. This pulls them
1540 * into the sysfs layer if needed, and can result in processing of 1579 * into the sysfs layer if needed, and can result in processing of
1541 * unit-directories, or just updating the node and it's 1580 * unit-directories, or just updating the node and it's
@@ -1545,19 +1584,7 @@ static void nodemgr_node_probe(struct host_info *hi, int generation)
1545 * while probes are time-consuming. (Well, those probes need some 1584 * while probes are time-consuming. (Well, those probes need some
1546 * improvement...) */ 1585 * improvement...) */
1547 1586
1548 down(&nodemgr_ne_class.sem); 1587 class_for_each_device(&nodemgr_ne_class, &param, __nodemgr_node_probe);
1549 list_for_each_entry(dev, &nodemgr_ne_class.devices, node) {
1550 ne = container_of(dev, struct node_entry, node_dev);
1551 if (!ne->needs_probe)
1552 nodemgr_probe_ne(hi, ne, generation);
1553 }
1554 list_for_each_entry(dev, &nodemgr_ne_class.devices, node) {
1555 ne = container_of(dev, struct node_entry, node_dev);
1556 if (ne->needs_probe)
1557 nodemgr_probe_ne(hi, ne, generation);
1558 }
1559 up(&nodemgr_ne_class.sem);
1560
1561 1588
1562 /* If we had a bus reset while we were scanning the bus, it is 1589 /* If we had a bus reset while we were scanning the bus, it is
1563 * possible that we did not probe all nodes. In that case, we 1590 * possible that we did not probe all nodes. In that case, we
@@ -1757,6 +1784,22 @@ exit:
1757 return 0; 1784 return 0;
1758} 1785}
1759 1786
1787struct host_iter_param {
1788 void *data;
1789 int (*cb)(struct hpsb_host *, void *);
1790};
1791
1792static int __nodemgr_for_each_host(struct device *dev, void *data)
1793{
1794 struct hpsb_host *host;
1795 struct host_iter_param *hip = (struct host_iter_param *)data;
1796 int error = 0;
1797
1798 host = container_of(dev, struct hpsb_host, host_dev);
1799 error = hip->cb(host, hip->data);
1800
1801 return error;
1802}
1760/** 1803/**
1761 * nodemgr_for_each_host - call a function for each IEEE 1394 host 1804 * nodemgr_for_each_host - call a function for each IEEE 1394 host
1762 * @data: an address to supply to the callback 1805 * @data: an address to supply to the callback
@@ -1771,18 +1814,13 @@ exit:
1771 */ 1814 */
1772int nodemgr_for_each_host(void *data, int (*cb)(struct hpsb_host *, void *)) 1815int nodemgr_for_each_host(void *data, int (*cb)(struct hpsb_host *, void *))
1773{ 1816{
1774 struct device *dev; 1817 struct host_iter_param hip;
1775 struct hpsb_host *host; 1818 int error;
1776 int error = 0;
1777
1778 down(&hpsb_host_class.sem);
1779 list_for_each_entry(dev, &hpsb_host_class.devices, node) {
1780 host = container_of(dev, struct hpsb_host, host_dev);
1781 1819
1782 if ((error = cb(host, data))) 1820 hip.cb = cb;
1783 break; 1821 hip.data = data;
1784 } 1822 error = class_for_each_device(&hpsb_host_class, &hip,
1785 up(&hpsb_host_class.sem); 1823 __nodemgr_for_each_host);
1786 1824
1787 return error; 1825 return error;
1788} 1826}
diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c
index 3d4050681325..c864ef70fdf9 100644
--- a/drivers/infiniband/core/sysfs.c
+++ b/drivers/infiniband/core/sysfs.c
@@ -508,19 +508,10 @@ static int add_port(struct ib_device *device, int port_num)
508 508
509 p->ibdev = device; 509 p->ibdev = device;
510 p->port_num = port_num; 510 p->port_num = port_num;
511 p->kobj.ktype = &port_type;
512 511
513 p->kobj.parent = kobject_get(&device->ports_parent); 512 ret = kobject_init_and_add(&p->kobj, &port_type,
514 if (!p->kobj.parent) { 513 kobject_get(device->ports_parent),
515 ret = -EBUSY; 514 "%d", port_num);
516 goto err;
517 }
518
519 ret = kobject_set_name(&p->kobj, "%d", port_num);
520 if (ret)
521 goto err_put;
522
523 ret = kobject_register(&p->kobj);
524 if (ret) 515 if (ret)
525 goto err_put; 516 goto err_put;
526 517
@@ -549,6 +540,7 @@ static int add_port(struct ib_device *device, int port_num)
549 540
550 list_add_tail(&p->kobj.entry, &device->port_list); 541 list_add_tail(&p->kobj.entry, &device->port_list);
551 542
543 kobject_uevent(&p->kobj, KOBJ_ADD);
552 return 0; 544 return 0;
553 545
554err_free_pkey: 546err_free_pkey:
@@ -570,9 +562,7 @@ err_remove_pma:
570 sysfs_remove_group(&p->kobj, &pma_group); 562 sysfs_remove_group(&p->kobj, &pma_group);
571 563
572err_put: 564err_put:
573 kobject_put(&device->ports_parent); 565 kobject_put(device->ports_parent);
574
575err:
576 kfree(p); 566 kfree(p);
577 return ret; 567 return ret;
578} 568}
@@ -694,16 +684,9 @@ int ib_device_register_sysfs(struct ib_device *device)
694 goto err_unregister; 684 goto err_unregister;
695 } 685 }
696 686
697 device->ports_parent.parent = kobject_get(&class_dev->kobj); 687 device->ports_parent = kobject_create_and_add("ports",
698 if (!device->ports_parent.parent) { 688 kobject_get(&class_dev->kobj));
699 ret = -EBUSY; 689 if (!device->ports_parent)
700 goto err_unregister;
701 }
702 ret = kobject_set_name(&device->ports_parent, "ports");
703 if (ret)
704 goto err_put;
705 ret = kobject_register(&device->ports_parent);
706 if (ret)
707 goto err_put; 690 goto err_put;
708 691
709 if (device->node_type == RDMA_NODE_IB_SWITCH) { 692 if (device->node_type == RDMA_NODE_IB_SWITCH) {
@@ -731,7 +714,7 @@ err_put:
731 sysfs_remove_group(p, &pma_group); 714 sysfs_remove_group(p, &pma_group);
732 sysfs_remove_group(p, &port->pkey_group); 715 sysfs_remove_group(p, &port->pkey_group);
733 sysfs_remove_group(p, &port->gid_group); 716 sysfs_remove_group(p, &port->gid_group);
734 kobject_unregister(p); 717 kobject_put(p);
735 } 718 }
736 } 719 }
737 720
@@ -755,10 +738,10 @@ void ib_device_unregister_sysfs(struct ib_device *device)
755 sysfs_remove_group(p, &pma_group); 738 sysfs_remove_group(p, &pma_group);
756 sysfs_remove_group(p, &port->pkey_group); 739 sysfs_remove_group(p, &port->pkey_group);
757 sysfs_remove_group(p, &port->gid_group); 740 sysfs_remove_group(p, &port->gid_group);
758 kobject_unregister(p); 741 kobject_put(p);
759 } 742 }
760 743
761 kobject_unregister(&device->ports_parent); 744 kobject_put(device->ports_parent);
762 class_device_unregister(&device->class_dev); 745 class_device_unregister(&device->class_dev);
763} 746}
764 747
diff --git a/drivers/infiniband/hw/ehca/ehca_main.c b/drivers/infiniband/hw/ehca/ehca_main.c
index 6a56d86a2951..c9e32b46387f 100644
--- a/drivers/infiniband/hw/ehca/ehca_main.c
+++ b/drivers/infiniband/hw/ehca/ehca_main.c
@@ -590,6 +590,11 @@ static struct attribute_group ehca_drv_attr_grp = {
590 .attrs = ehca_drv_attrs 590 .attrs = ehca_drv_attrs
591}; 591};
592 592
593static struct attribute_group *ehca_drv_attr_groups[] = {
594 &ehca_drv_attr_grp,
595 NULL,
596};
597
593#define EHCA_RESOURCE_ATTR(name) \ 598#define EHCA_RESOURCE_ATTR(name) \
594static ssize_t ehca_show_##name(struct device *dev, \ 599static ssize_t ehca_show_##name(struct device *dev, \
595 struct device_attribute *attr, \ 600 struct device_attribute *attr, \
@@ -899,6 +904,9 @@ static struct of_platform_driver ehca_driver = {
899 .match_table = ehca_device_table, 904 .match_table = ehca_device_table,
900 .probe = ehca_probe, 905 .probe = ehca_probe,
901 .remove = ehca_remove, 906 .remove = ehca_remove,
907 .driver = {
908 .groups = ehca_drv_attr_groups,
909 },
902}; 910};
903 911
904void ehca_poll_eqs(unsigned long data) 912void ehca_poll_eqs(unsigned long data)
@@ -957,10 +965,6 @@ int __init ehca_module_init(void)
957 goto module_init2; 965 goto module_init2;
958 } 966 }
959 967
960 ret = sysfs_create_group(&ehca_driver.driver.kobj, &ehca_drv_attr_grp);
961 if (ret) /* only complain; we can live without attributes */
962 ehca_gen_err("Cannot create driver attributes ret=%d", ret);
963
964 if (ehca_poll_all_eqs != 1) { 968 if (ehca_poll_all_eqs != 1) {
965 ehca_gen_err("WARNING!!!"); 969 ehca_gen_err("WARNING!!!");
966 ehca_gen_err("It is possible to lose interrupts."); 970 ehca_gen_err("It is possible to lose interrupts.");
@@ -986,7 +990,6 @@ void __exit ehca_module_exit(void)
986 if (ehca_poll_all_eqs == 1) 990 if (ehca_poll_all_eqs == 1)
987 del_timer_sync(&poll_eqs_timer); 991 del_timer_sync(&poll_eqs_timer);
988 992
989 sysfs_remove_group(&ehca_driver.driver.kobj, &ehca_drv_attr_grp);
990 ibmebus_unregister_driver(&ehca_driver); 993 ibmebus_unregister_driver(&ehca_driver);
991 994
992 ehca_destroy_slab_caches(); 995 ehca_destroy_slab_caches();
diff --git a/drivers/infiniband/hw/ipath/ipath_driver.c b/drivers/infiniband/hw/ipath/ipath_driver.c
index 1f152ded1e3c..fc355981bbab 100644
--- a/drivers/infiniband/hw/ipath/ipath_driver.c
+++ b/drivers/infiniband/hw/ipath/ipath_driver.c
@@ -121,6 +121,9 @@ static struct pci_driver ipath_driver = {
121 .probe = ipath_init_one, 121 .probe = ipath_init_one,
122 .remove = __devexit_p(ipath_remove_one), 122 .remove = __devexit_p(ipath_remove_one),
123 .id_table = ipath_pci_tbl, 123 .id_table = ipath_pci_tbl,
124 .driver = {
125 .groups = ipath_driver_attr_groups,
126 },
124}; 127};
125 128
126static void ipath_check_status(struct work_struct *work) 129static void ipath_check_status(struct work_struct *work)
@@ -2217,25 +2220,15 @@ static int __init infinipath_init(void)
2217 goto bail_unit; 2220 goto bail_unit;
2218 } 2221 }
2219 2222
2220 ret = ipath_driver_create_group(&ipath_driver.driver);
2221 if (ret < 0) {
2222 printk(KERN_ERR IPATH_DRV_NAME ": Unable to create driver "
2223 "sysfs entries: error %d\n", -ret);
2224 goto bail_pci;
2225 }
2226
2227 ret = ipath_init_ipathfs(); 2223 ret = ipath_init_ipathfs();
2228 if (ret < 0) { 2224 if (ret < 0) {
2229 printk(KERN_ERR IPATH_DRV_NAME ": Unable to create " 2225 printk(KERN_ERR IPATH_DRV_NAME ": Unable to create "
2230 "ipathfs: error %d\n", -ret); 2226 "ipathfs: error %d\n", -ret);
2231 goto bail_group; 2227 goto bail_pci;
2232 } 2228 }
2233 2229
2234 goto bail; 2230 goto bail;
2235 2231
2236bail_group:
2237 ipath_driver_remove_group(&ipath_driver.driver);
2238
2239bail_pci: 2232bail_pci:
2240 pci_unregister_driver(&ipath_driver); 2233 pci_unregister_driver(&ipath_driver);
2241 2234
@@ -2250,8 +2243,6 @@ static void __exit infinipath_cleanup(void)
2250{ 2243{
2251 ipath_exit_ipathfs(); 2244 ipath_exit_ipathfs();
2252 2245
2253 ipath_driver_remove_group(&ipath_driver.driver);
2254
2255 ipath_cdbg(VERBOSE, "Unregistering pci driver\n"); 2246 ipath_cdbg(VERBOSE, "Unregistering pci driver\n");
2256 pci_unregister_driver(&ipath_driver); 2247 pci_unregister_driver(&ipath_driver);
2257 2248
diff --git a/drivers/infiniband/hw/ipath/ipath_kernel.h b/drivers/infiniband/hw/ipath/ipath_kernel.h
index 8786dd7922e4..bb1dc075f1d1 100644
--- a/drivers/infiniband/hw/ipath/ipath_kernel.h
+++ b/drivers/infiniband/hw/ipath/ipath_kernel.h
@@ -938,8 +938,7 @@ struct device_driver;
938 938
939extern const char ib_ipath_version[]; 939extern const char ib_ipath_version[];
940 940
941int ipath_driver_create_group(struct device_driver *); 941extern struct attribute_group *ipath_driver_attr_groups[];
942void ipath_driver_remove_group(struct device_driver *);
943 942
944int ipath_device_create_group(struct device *, struct ipath_devdata *); 943int ipath_device_create_group(struct device *, struct ipath_devdata *);
945void ipath_device_remove_group(struct device *, struct ipath_devdata *); 944void ipath_device_remove_group(struct device *, struct ipath_devdata *);
diff --git a/drivers/infiniband/hw/ipath/ipath_sysfs.c b/drivers/infiniband/hw/ipath/ipath_sysfs.c
index e1ad7cfc21fd..aa27ca9f03b1 100644
--- a/drivers/infiniband/hw/ipath/ipath_sysfs.c
+++ b/drivers/infiniband/hw/ipath/ipath_sysfs.c
@@ -683,6 +683,11 @@ static struct attribute_group driver_attr_group = {
683 .attrs = driver_attributes 683 .attrs = driver_attributes
684}; 684};
685 685
686struct attribute_group *ipath_driver_attr_groups[] = {
687 &driver_attr_group,
688 NULL,
689};
690
686static DEVICE_ATTR(guid, S_IWUSR | S_IRUGO, show_guid, store_guid); 691static DEVICE_ATTR(guid, S_IWUSR | S_IRUGO, show_guid, store_guid);
687static DEVICE_ATTR(lmc, S_IWUSR | S_IRUGO, show_lmc, store_lmc); 692static DEVICE_ATTR(lmc, S_IWUSR | S_IRUGO, show_lmc, store_lmc);
688static DEVICE_ATTR(lid, S_IWUSR | S_IRUGO, show_lid, store_lid); 693static DEVICE_ATTR(lid, S_IWUSR | S_IRUGO, show_lid, store_lid);
@@ -753,24 +758,9 @@ int ipath_expose_reset(struct device *dev)
753 return ret; 758 return ret;
754} 759}
755 760
756int ipath_driver_create_group(struct device_driver *drv)
757{
758 int ret;
759
760 ret = sysfs_create_group(&drv->kobj, &driver_attr_group);
761
762 return ret;
763}
764
765void ipath_driver_remove_group(struct device_driver *drv)
766{
767 sysfs_remove_group(&drv->kobj, &driver_attr_group);
768}
769
770int ipath_device_create_group(struct device *dev, struct ipath_devdata *dd) 761int ipath_device_create_group(struct device *dev, struct ipath_devdata *dd)
771{ 762{
772 int ret; 763 int ret;
773 char unit[5];
774 764
775 ret = sysfs_create_group(&dev->kobj, &dev_attr_group); 765 ret = sysfs_create_group(&dev->kobj, &dev_attr_group);
776 if (ret) 766 if (ret)
@@ -780,11 +770,6 @@ int ipath_device_create_group(struct device *dev, struct ipath_devdata *dd)
780 if (ret) 770 if (ret)
781 goto bail_attrs; 771 goto bail_attrs;
782 772
783 snprintf(unit, sizeof(unit), "%02d", dd->ipath_unit);
784 ret = sysfs_create_link(&dev->driver->kobj, &dev->kobj, unit);
785 if (ret == 0)
786 goto bail;
787
788 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group); 773 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
789bail_attrs: 774bail_attrs:
790 sysfs_remove_group(&dev->kobj, &dev_attr_group); 775 sysfs_remove_group(&dev->kobj, &dev_attr_group);
@@ -794,11 +779,6 @@ bail:
794 779
795void ipath_device_remove_group(struct device *dev, struct ipath_devdata *dd) 780void ipath_device_remove_group(struct device *dev, struct ipath_devdata *dd)
796{ 781{
797 char unit[5];
798
799 snprintf(unit, sizeof(unit), "%02d", dd->ipath_unit);
800 sysfs_remove_link(&dev->driver->kobj, unit);
801
802 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group); 782 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
803 sysfs_remove_group(&dev->kobj, &dev_attr_group); 783 sysfs_remove_group(&dev->kobj, &dev_attr_group);
804 784
diff --git a/drivers/isdn/capi/capi.c b/drivers/isdn/capi/capi.c
index f449daef3eed..23ae66c76d47 100644
--- a/drivers/isdn/capi/capi.c
+++ b/drivers/isdn/capi/capi.c
@@ -1544,11 +1544,11 @@ static int __init capi_init(void)
1544 return PTR_ERR(capi_class); 1544 return PTR_ERR(capi_class);
1545 } 1545 }
1546 1546
1547 class_device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi"); 1547 device_create(capi_class, NULL, MKDEV(capi_major, 0), "capi");
1548 1548
1549#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE 1549#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1550 if (capinc_tty_init() < 0) { 1550 if (capinc_tty_init() < 0) {
1551 class_device_destroy(capi_class, MKDEV(capi_major, 0)); 1551 device_destroy(capi_class, MKDEV(capi_major, 0));
1552 class_destroy(capi_class); 1552 class_destroy(capi_class);
1553 unregister_chrdev(capi_major, "capi20"); 1553 unregister_chrdev(capi_major, "capi20");
1554 return -ENOMEM; 1554 return -ENOMEM;
@@ -1576,7 +1576,7 @@ static void __exit capi_exit(void)
1576{ 1576{
1577 proc_exit(); 1577 proc_exit();
1578 1578
1579 class_device_destroy(capi_class, MKDEV(capi_major, 0)); 1579 device_destroy(capi_class, MKDEV(capi_major, 0));
1580 class_destroy(capi_class); 1580 class_destroy(capi_class);
1581 unregister_chrdev(capi_major, "capi20"); 1581 unregister_chrdev(capi_major, "capi20");
1582 1582
diff --git a/drivers/isdn/gigaset/gigaset.h b/drivers/isdn/gigaset/gigaset.h
index a0317abaeb11..02bdaf22d7ea 100644
--- a/drivers/isdn/gigaset/gigaset.h
+++ b/drivers/isdn/gigaset/gigaset.h
@@ -106,12 +106,6 @@ enum debuglevel {
106 activated */ 106 activated */
107}; 107};
108 108
109/* missing from linux/device.h ... */
110#ifndef dev_notice
111#define dev_notice(dev, format, arg...) \
112 dev_printk(KERN_NOTICE , dev , format , ## arg)
113#endif
114
115/* Kernel message macros for situations where dev_printk and friends cannot be 109/* Kernel message macros for situations where dev_printk and friends cannot be
116 * used for lack of reliable access to a device structure. 110 * used for lack of reliable access to a device structure.
117 * linux/usb.h already contains these but in an obsolete form which clutters 111 * linux/usb.h already contains these but in an obsolete form which clutters
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index 47c10b8f89b3..c0f372f1d761 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -3451,7 +3451,7 @@ static int kvm_resume(struct sys_device *dev)
3451} 3451}
3452 3452
3453static struct sysdev_class kvm_sysdev_class = { 3453static struct sysdev_class kvm_sysdev_class = {
3454 set_kset_name("kvm"), 3454 .name = "kvm",
3455 .suspend = kvm_suspend, 3455 .suspend = kvm_suspend,
3456 .resume = kvm_resume, 3456 .resume = kvm_resume,
3457}; 3457};
diff --git a/drivers/macintosh/adb.c b/drivers/macintosh/adb.c
index 5c742a526082..b7adde4324e4 100644
--- a/drivers/macintosh/adb.c
+++ b/drivers/macintosh/adb.c
@@ -875,5 +875,5 @@ adbdev_init(void)
875 adb_dev_class = class_create(THIS_MODULE, "adb"); 875 adb_dev_class = class_create(THIS_MODULE, "adb");
876 if (IS_ERR(adb_dev_class)) 876 if (IS_ERR(adb_dev_class))
877 return; 877 return;
878 class_device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb"); 878 device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), "adb");
879} 879}
diff --git a/drivers/macintosh/via-pmu.c b/drivers/macintosh/via-pmu.c
index 6123c70153d3..ac420b17e16f 100644
--- a/drivers/macintosh/via-pmu.c
+++ b/drivers/macintosh/via-pmu.c
@@ -2796,7 +2796,7 @@ static int pmu_sys_resume(struct sys_device *sysdev)
2796#endif /* CONFIG_PM_SLEEP && CONFIG_PPC32 */ 2796#endif /* CONFIG_PM_SLEEP && CONFIG_PPC32 */
2797 2797
2798static struct sysdev_class pmu_sysclass = { 2798static struct sysdev_class pmu_sysclass = {
2799 set_kset_name("pmu"), 2799 .name = "pmu",
2800}; 2800};
2801 2801
2802static struct sys_device device_pmu = { 2802static struct sys_device device_pmu = {
diff --git a/drivers/md/dm.c b/drivers/md/dm.c
index 88c0fd657825..f2d24eb3208c 100644
--- a/drivers/md/dm.c
+++ b/drivers/md/dm.c
@@ -1109,7 +1109,7 @@ static void event_callback(void *context)
1109 list_splice_init(&md->uevent_list, &uevents); 1109 list_splice_init(&md->uevent_list, &uevents);
1110 spin_unlock_irqrestore(&md->uevent_lock, flags); 1110 spin_unlock_irqrestore(&md->uevent_lock, flags);
1111 1111
1112 dm_send_uevents(&uevents, &md->disk->kobj); 1112 dm_send_uevents(&uevents, &md->disk->dev.kobj);
1113 1113
1114 atomic_inc(&md->event_nr); 1114 atomic_inc(&md->event_nr);
1115 wake_up(&md->eventq); 1115 wake_up(&md->eventq);
@@ -1530,7 +1530,7 @@ out:
1530 *---------------------------------------------------------------*/ 1530 *---------------------------------------------------------------*/
1531void dm_kobject_uevent(struct mapped_device *md) 1531void dm_kobject_uevent(struct mapped_device *md)
1532{ 1532{
1533 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE); 1533 kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE);
1534} 1534}
1535 1535
1536uint32_t dm_next_uevent_seq(struct mapped_device *md) 1536uint32_t dm_next_uevent_seq(struct mapped_device *md)
diff --git a/drivers/md/md.c b/drivers/md/md.c
index cef9ebd5a046..c28a120b4161 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -231,7 +231,7 @@ static void mddev_put(mddev_t *mddev)
231 list_del(&mddev->all_mddevs); 231 list_del(&mddev->all_mddevs);
232 spin_unlock(&all_mddevs_lock); 232 spin_unlock(&all_mddevs_lock);
233 blk_cleanup_queue(mddev->queue); 233 blk_cleanup_queue(mddev->queue);
234 kobject_unregister(&mddev->kobj); 234 kobject_put(&mddev->kobj);
235 } else 235 } else
236 spin_unlock(&all_mddevs_lock); 236 spin_unlock(&all_mddevs_lock);
237} 237}
@@ -1383,22 +1383,19 @@ static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1383 return -EBUSY; 1383 return -EBUSY;
1384 } 1384 }
1385 bdevname(rdev->bdev,b); 1385 bdevname(rdev->bdev,b);
1386 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0) 1386 while ( (s=strchr(b, '/')) != NULL)
1387 return -ENOMEM;
1388 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1389 *s = '!'; 1387 *s = '!';
1390 1388
1391 rdev->mddev = mddev; 1389 rdev->mddev = mddev;
1392 printk(KERN_INFO "md: bind<%s>\n", b); 1390 printk(KERN_INFO "md: bind<%s>\n", b);
1393 1391
1394 rdev->kobj.parent = &mddev->kobj; 1392 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1395 if ((err = kobject_add(&rdev->kobj)))
1396 goto fail; 1393 goto fail;
1397 1394
1398 if (rdev->bdev->bd_part) 1395 if (rdev->bdev->bd_part)
1399 ko = &rdev->bdev->bd_part->kobj; 1396 ko = &rdev->bdev->bd_part->dev.kobj;
1400 else 1397 else
1401 ko = &rdev->bdev->bd_disk->kobj; 1398 ko = &rdev->bdev->bd_disk->dev.kobj;
1402 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { 1399 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1403 kobject_del(&rdev->kobj); 1400 kobject_del(&rdev->kobj);
1404 goto fail; 1401 goto fail;
@@ -2036,9 +2033,7 @@ static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_mi
2036 if (err) 2033 if (err)
2037 goto abort_free; 2034 goto abort_free;
2038 2035
2039 rdev->kobj.parent = NULL; 2036 kobject_init(&rdev->kobj, &rdev_ktype);
2040 rdev->kobj.ktype = &rdev_ktype;
2041 kobject_init(&rdev->kobj);
2042 2037
2043 rdev->desc_nr = -1; 2038 rdev->desc_nr = -1;
2044 rdev->saved_raid_disk = -1; 2039 rdev->saved_raid_disk = -1;
@@ -3054,6 +3049,7 @@ static struct kobject *md_probe(dev_t dev, int *part, void *data)
3054 int partitioned = (MAJOR(dev) != MD_MAJOR); 3049 int partitioned = (MAJOR(dev) != MD_MAJOR);
3055 int shift = partitioned ? MdpMinorShift : 0; 3050 int shift = partitioned ? MdpMinorShift : 0;
3056 int unit = MINOR(dev) >> shift; 3051 int unit = MINOR(dev) >> shift;
3052 int error;
3057 3053
3058 if (!mddev) 3054 if (!mddev)
3059 return NULL; 3055 return NULL;
@@ -3082,12 +3078,13 @@ static struct kobject *md_probe(dev_t dev, int *part, void *data)
3082 add_disk(disk); 3078 add_disk(disk);
3083 mddev->gendisk = disk; 3079 mddev->gendisk = disk;
3084 mutex_unlock(&disks_mutex); 3080 mutex_unlock(&disks_mutex);
3085 mddev->kobj.parent = &disk->kobj; 3081 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
3086 kobject_set_name(&mddev->kobj, "%s", "md"); 3082 "%s", "md");
3087 mddev->kobj.ktype = &md_ktype; 3083 if (error)
3088 if (kobject_register(&mddev->kobj))
3089 printk(KERN_WARNING "md: cannot register %s/md - name in use\n", 3084 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3090 disk->disk_name); 3085 disk->disk_name);
3086 else
3087 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3091 return NULL; 3088 return NULL;
3092} 3089}
3093 3090
@@ -3359,7 +3356,7 @@ static int do_md_run(mddev_t * mddev)
3359 3356
3360 mddev->changed = 1; 3357 mddev->changed = 1;
3361 md_new_event(mddev); 3358 md_new_event(mddev);
3362 kobject_uevent(&mddev->gendisk->kobj, KOBJ_CHANGE); 3359 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
3363 return 0; 3360 return 0;
3364} 3361}
3365 3362
diff --git a/drivers/mfd/ucb1x00-assabet.c b/drivers/mfd/ucb1x00-assabet.c
index e325fa71f38b..b7c8e7813865 100644
--- a/drivers/mfd/ucb1x00-assabet.c
+++ b/drivers/mfd/ucb1x00-assabet.c
@@ -20,7 +20,8 @@
20#include "ucb1x00.h" 20#include "ucb1x00.h"
21 21
22#define UCB1X00_ATTR(name,input)\ 22#define UCB1X00_ATTR(name,input)\
23static ssize_t name##_show(struct class_device *dev, char *buf) \ 23static ssize_t name##_show(struct device *dev, struct device_attribute *attr,
24 char *buf) \
24{ \ 25{ \
25 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev); \ 26 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev); \
26 int val; \ 27 int val; \
@@ -29,7 +30,7 @@ static ssize_t name##_show(struct class_device *dev, char *buf) \
29 ucb1x00_adc_disable(ucb); \ 30 ucb1x00_adc_disable(ucb); \
30 return sprintf(buf, "%d\n", val); \ 31 return sprintf(buf, "%d\n", val); \
31} \ 32} \
32static CLASS_DEVICE_ATTR(name,0444,name##_show,NULL) 33static DEVICE_ATTR(name,0444,name##_show,NULL)
33 34
34UCB1X00_ATTR(vbatt, UCB_ADC_INP_AD1); 35UCB1X00_ATTR(vbatt, UCB_ADC_INP_AD1);
35UCB1X00_ATTR(vcharger, UCB_ADC_INP_AD0); 36UCB1X00_ATTR(vcharger, UCB_ADC_INP_AD0);
@@ -37,17 +38,17 @@ UCB1X00_ATTR(batt_temp, UCB_ADC_INP_AD2);
37 38
38static int ucb1x00_assabet_add(struct ucb1x00_dev *dev) 39static int ucb1x00_assabet_add(struct ucb1x00_dev *dev)
39{ 40{
40 class_device_create_file(&dev->ucb->cdev, &class_device_attr_vbatt); 41 device_create_file(&dev->ucb->dev, &device_attr_vbatt);
41 class_device_create_file(&dev->ucb->cdev, &class_device_attr_vcharger); 42 device_create_file(&dev->ucb->dev, &device_attr_vcharger);
42 class_device_create_file(&dev->ucb->cdev, &class_device_attr_batt_temp); 43 device_create_file(&dev->ucb->dev, &device_attr_batt_temp);
43 return 0; 44 return 0;
44} 45}
45 46
46static void ucb1x00_assabet_remove(struct ucb1x00_dev *dev) 47static void ucb1x00_assabet_remove(struct ucb1x00_dev *dev)
47{ 48{
48 class_device_remove_file(&dev->ucb->cdev, &class_device_attr_batt_temp); 49 device_remove_file(&dev->ucb->cdev, &device_attr_batt_temp);
49 class_device_remove_file(&dev->ucb->cdev, &class_device_attr_vcharger); 50 device_remove_file(&dev->ucb->cdev, &device_attr_vcharger);
50 class_device_remove_file(&dev->ucb->cdev, &class_device_attr_vbatt); 51 device_remove_file(&dev->ucb->cdev, &device_attr_vbatt);
51} 52}
52 53
53static struct ucb1x00_driver ucb1x00_assabet_driver = { 54static struct ucb1x00_driver ucb1x00_assabet_driver = {
diff --git a/drivers/mfd/ucb1x00-core.c b/drivers/mfd/ucb1x00-core.c
index e03f1bcd4f9f..f6b10dda31fd 100644
--- a/drivers/mfd/ucb1x00-core.c
+++ b/drivers/mfd/ucb1x00-core.c
@@ -458,7 +458,7 @@ static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
458 return probe_irq_off(mask); 458 return probe_irq_off(mask);
459} 459}
460 460
461static void ucb1x00_release(struct class_device *dev) 461static void ucb1x00_release(struct device *dev)
462{ 462{
463 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev); 463 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
464 kfree(ucb); 464 kfree(ucb);
@@ -466,7 +466,7 @@ static void ucb1x00_release(struct class_device *dev)
466 466
467static struct class ucb1x00_class = { 467static struct class ucb1x00_class = {
468 .name = "ucb1x00", 468 .name = "ucb1x00",
469 .release = ucb1x00_release, 469 .dev_release = ucb1x00_release,
470}; 470};
471 471
472static int ucb1x00_probe(struct mcp *mcp) 472static int ucb1x00_probe(struct mcp *mcp)
@@ -490,9 +490,9 @@ static int ucb1x00_probe(struct mcp *mcp)
490 goto err_disable; 490 goto err_disable;
491 491
492 492
493 ucb->cdev.class = &ucb1x00_class; 493 ucb->dev.class = &ucb1x00_class;
494 ucb->cdev.dev = &mcp->attached_device; 494 ucb->dev.parent = &mcp->attached_device;
495 strlcpy(ucb->cdev.class_id, "ucb1x00", sizeof(ucb->cdev.class_id)); 495 strlcpy(ucb->dev.bus_id, "ucb1x00", sizeof(ucb->dev.bus_id));
496 496
497 spin_lock_init(&ucb->lock); 497 spin_lock_init(&ucb->lock);
498 spin_lock_init(&ucb->io_lock); 498 spin_lock_init(&ucb->io_lock);
@@ -517,7 +517,7 @@ static int ucb1x00_probe(struct mcp *mcp)
517 517
518 mcp_set_drvdata(mcp, ucb); 518 mcp_set_drvdata(mcp, ucb);
519 519
520 ret = class_device_register(&ucb->cdev); 520 ret = device_register(&ucb->dev);
521 if (ret) 521 if (ret)
522 goto err_irq; 522 goto err_irq;
523 523
@@ -554,7 +554,7 @@ static void ucb1x00_remove(struct mcp *mcp)
554 mutex_unlock(&ucb1x00_mutex); 554 mutex_unlock(&ucb1x00_mutex);
555 555
556 free_irq(ucb->irq, ucb); 556 free_irq(ucb->irq, ucb);
557 class_device_unregister(&ucb->cdev); 557 device_unregister(&ucb->dev);
558} 558}
559 559
560int ucb1x00_register_driver(struct ucb1x00_driver *drv) 560int ucb1x00_register_driver(struct ucb1x00_driver *drv)
diff --git a/drivers/mfd/ucb1x00.h b/drivers/mfd/ucb1x00.h
index ca8df8072d43..a8ad8a0ed5db 100644
--- a/drivers/mfd/ucb1x00.h
+++ b/drivers/mfd/ucb1x00.h
@@ -120,7 +120,7 @@ struct ucb1x00 {
120 u16 irq_fal_enbl; 120 u16 irq_fal_enbl;
121 u16 irq_ris_enbl; 121 u16 irq_ris_enbl;
122 struct ucb1x00_irq irq_handler[16]; 122 struct ucb1x00_irq irq_handler[16];
123 struct class_device cdev; 123 struct device dev;
124 struct list_head node; 124 struct list_head node;
125 struct list_head devs; 125 struct list_head devs;
126}; 126};
@@ -144,7 +144,7 @@ struct ucb1x00_driver {
144 int (*resume)(struct ucb1x00_dev *dev); 144 int (*resume)(struct ucb1x00_dev *dev);
145}; 145};
146 146
147#define classdev_to_ucb1x00(cd) container_of(cd, struct ucb1x00, cdev) 147#define classdev_to_ucb1x00(cd) container_of(cd, struct ucb1x00, dev)
148 148
149int ucb1x00_register_driver(struct ucb1x00_driver *); 149int ucb1x00_register_driver(struct ucb1x00_driver *);
150void ucb1x00_unregister_driver(struct ucb1x00_driver *); 150void ucb1x00_unregister_driver(struct ucb1x00_driver *);
diff --git a/drivers/misc/ibmasm/command.c b/drivers/misc/ibmasm/command.c
index 6497872df524..1a0e7978226a 100644
--- a/drivers/misc/ibmasm/command.c
+++ b/drivers/misc/ibmasm/command.c
@@ -26,11 +26,6 @@
26#include "lowlevel.h" 26#include "lowlevel.h"
27 27
28static void exec_next_command(struct service_processor *sp); 28static void exec_next_command(struct service_processor *sp);
29static void free_command(struct kobject *kobj);
30
31static struct kobj_type ibmasm_cmd_kobj_type = {
32 .release = free_command,
33};
34 29
35static atomic_t command_count = ATOMIC_INIT(0); 30static atomic_t command_count = ATOMIC_INIT(0);
36 31
@@ -53,8 +48,7 @@ struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_s
53 } 48 }
54 cmd->buffer_size = buffer_size; 49 cmd->buffer_size = buffer_size;
55 50
56 kobject_init(&cmd->kobj); 51 kref_init(&cmd->kref);
57 cmd->kobj.ktype = &ibmasm_cmd_kobj_type;
58 cmd->lock = &sp->lock; 52 cmd->lock = &sp->lock;
59 53
60 cmd->status = IBMASM_CMD_PENDING; 54 cmd->status = IBMASM_CMD_PENDING;
@@ -67,9 +61,9 @@ struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_s
67 return cmd; 61 return cmd;
68} 62}
69 63
70static void free_command(struct kobject *kobj) 64void ibmasm_free_command(struct kref *kref)
71{ 65{
72 struct command *cmd = to_command(kobj); 66 struct command *cmd = to_command(kref);
73 67
74 list_del(&cmd->queue_node); 68 list_del(&cmd->queue_node);
75 atomic_dec(&command_count); 69 atomic_dec(&command_count);
diff --git a/drivers/misc/ibmasm/ibmasm.h b/drivers/misc/ibmasm/ibmasm.h
index de860bc6d3f5..4d8a4e248b34 100644
--- a/drivers/misc/ibmasm/ibmasm.h
+++ b/drivers/misc/ibmasm/ibmasm.h
@@ -31,6 +31,7 @@
31#include <linux/slab.h> 31#include <linux/slab.h>
32#include <linux/module.h> 32#include <linux/module.h>
33#include <linux/interrupt.h> 33#include <linux/interrupt.h>
34#include <linux/kref.h>
34#include <linux/device.h> 35#include <linux/device.h>
35#include <linux/input.h> 36#include <linux/input.h>
36 37
@@ -92,24 +93,25 @@ struct command {
92 unsigned char *buffer; 93 unsigned char *buffer;
93 size_t buffer_size; 94 size_t buffer_size;
94 int status; 95 int status;
95 struct kobject kobj; 96 struct kref kref;
96 spinlock_t *lock; 97 spinlock_t *lock;
97}; 98};
98#define to_command(c) container_of(c, struct command, kobj) 99#define to_command(c) container_of(c, struct command, kref)
99 100
101void ibmasm_free_command(struct kref *kref);
100static inline void command_put(struct command *cmd) 102static inline void command_put(struct command *cmd)
101{ 103{
102 unsigned long flags; 104 unsigned long flags;
103 spinlock_t *lock = cmd->lock; 105 spinlock_t *lock = cmd->lock;
104 106
105 spin_lock_irqsave(lock, flags); 107 spin_lock_irqsave(lock, flags);
106 kobject_put(&cmd->kobj); 108 kref_put(&cmd->kref, ibmasm_free_command);
107 spin_unlock_irqrestore(lock, flags); 109 spin_unlock_irqrestore(lock, flags);
108} 110}
109 111
110static inline void command_get(struct command *cmd) 112static inline void command_get(struct command *cmd)
111{ 113{
112 kobject_get(&cmd->kobj); 114 kref_get(&cmd->kref);
113} 115}
114 116
115 117
diff --git a/drivers/misc/tifm_7xx1.c b/drivers/misc/tifm_7xx1.c
index 2d1b3df95c5b..54380da343a5 100644
--- a/drivers/misc/tifm_7xx1.c
+++ b/drivers/misc/tifm_7xx1.c
@@ -149,7 +149,7 @@ static void tifm_7xx1_switch_media(struct work_struct *work)
149 socket_change_set = fm->socket_change_set; 149 socket_change_set = fm->socket_change_set;
150 fm->socket_change_set = 0; 150 fm->socket_change_set = 0;
151 151
152 dev_dbg(fm->cdev.dev, "checking media set %x\n", 152 dev_dbg(fm->dev.parent, "checking media set %x\n",
153 socket_change_set); 153 socket_change_set);
154 154
155 if (!socket_change_set) { 155 if (!socket_change_set) {
@@ -164,7 +164,7 @@ static void tifm_7xx1_switch_media(struct work_struct *work)
164 if (sock) { 164 if (sock) {
165 printk(KERN_INFO 165 printk(KERN_INFO
166 "%s : demand removing card from socket %u:%u\n", 166 "%s : demand removing card from socket %u:%u\n",
167 fm->cdev.class_id, fm->id, cnt); 167 fm->dev.bus_id, fm->id, cnt);
168 fm->sockets[cnt] = NULL; 168 fm->sockets[cnt] = NULL;
169 sock_addr = sock->addr; 169 sock_addr = sock->addr;
170 spin_unlock_irqrestore(&fm->lock, flags); 170 spin_unlock_irqrestore(&fm->lock, flags);
diff --git a/drivers/misc/tifm_core.c b/drivers/misc/tifm_core.c
index 8f77949f93dd..97544052e768 100644
--- a/drivers/misc/tifm_core.c
+++ b/drivers/misc/tifm_core.c
@@ -160,16 +160,16 @@ static struct bus_type tifm_bus_type = {
160 .resume = tifm_device_resume 160 .resume = tifm_device_resume
161}; 161};
162 162
163static void tifm_free(struct class_device *cdev) 163static void tifm_free(struct device *dev)
164{ 164{
165 struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev); 165 struct tifm_adapter *fm = container_of(dev, struct tifm_adapter, dev);
166 166
167 kfree(fm); 167 kfree(fm);
168} 168}
169 169
170static struct class tifm_adapter_class = { 170static struct class tifm_adapter_class = {
171 .name = "tifm_adapter", 171 .name = "tifm_adapter",
172 .release = tifm_free 172 .dev_release = tifm_free
173}; 173};
174 174
175struct tifm_adapter *tifm_alloc_adapter(unsigned int num_sockets, 175struct tifm_adapter *tifm_alloc_adapter(unsigned int num_sockets,
@@ -180,9 +180,9 @@ struct tifm_adapter *tifm_alloc_adapter(unsigned int num_sockets,
180 fm = kzalloc(sizeof(struct tifm_adapter) 180 fm = kzalloc(sizeof(struct tifm_adapter)
181 + sizeof(struct tifm_dev*) * num_sockets, GFP_KERNEL); 181 + sizeof(struct tifm_dev*) * num_sockets, GFP_KERNEL);
182 if (fm) { 182 if (fm) {
183 fm->cdev.class = &tifm_adapter_class; 183 fm->dev.class = &tifm_adapter_class;
184 fm->cdev.dev = dev; 184 fm->dev.parent = dev;
185 class_device_initialize(&fm->cdev); 185 device_initialize(&fm->dev);
186 spin_lock_init(&fm->lock); 186 spin_lock_init(&fm->lock);
187 fm->num_sockets = num_sockets; 187 fm->num_sockets = num_sockets;
188 } 188 }
@@ -203,8 +203,8 @@ int tifm_add_adapter(struct tifm_adapter *fm)
203 if (rc) 203 if (rc)
204 return rc; 204 return rc;
205 205
206 snprintf(fm->cdev.class_id, BUS_ID_SIZE, "tifm%u", fm->id); 206 snprintf(fm->dev.bus_id, BUS_ID_SIZE, "tifm%u", fm->id);
207 rc = class_device_add(&fm->cdev); 207 rc = device_add(&fm->dev);
208 if (rc) { 208 if (rc) {
209 spin_lock(&tifm_adapter_lock); 209 spin_lock(&tifm_adapter_lock);
210 idr_remove(&tifm_adapter_idr, fm->id); 210 idr_remove(&tifm_adapter_idr, fm->id);
@@ -228,13 +228,13 @@ void tifm_remove_adapter(struct tifm_adapter *fm)
228 spin_lock(&tifm_adapter_lock); 228 spin_lock(&tifm_adapter_lock);
229 idr_remove(&tifm_adapter_idr, fm->id); 229 idr_remove(&tifm_adapter_idr, fm->id);
230 spin_unlock(&tifm_adapter_lock); 230 spin_unlock(&tifm_adapter_lock);
231 class_device_del(&fm->cdev); 231 device_del(&fm->dev);
232} 232}
233EXPORT_SYMBOL(tifm_remove_adapter); 233EXPORT_SYMBOL(tifm_remove_adapter);
234 234
235void tifm_free_adapter(struct tifm_adapter *fm) 235void tifm_free_adapter(struct tifm_adapter *fm)
236{ 236{
237 class_device_put(&fm->cdev); 237 put_device(&fm->dev);
238} 238}
239EXPORT_SYMBOL(tifm_free_adapter); 239EXPORT_SYMBOL(tifm_free_adapter);
240 240
@@ -261,9 +261,9 @@ struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm, unsigned int id,
261 sock->card_event = tifm_dummy_event; 261 sock->card_event = tifm_dummy_event;
262 sock->data_event = tifm_dummy_event; 262 sock->data_event = tifm_dummy_event;
263 263
264 sock->dev.parent = fm->cdev.dev; 264 sock->dev.parent = fm->dev.parent;
265 sock->dev.bus = &tifm_bus_type; 265 sock->dev.bus = &tifm_bus_type;
266 sock->dev.dma_mask = fm->cdev.dev->dma_mask; 266 sock->dev.dma_mask = fm->dev.parent->dma_mask;
267 sock->dev.release = tifm_free_device; 267 sock->dev.release = tifm_free_device;
268 268
269 snprintf(sock->dev.bus_id, BUS_ID_SIZE, 269 snprintf(sock->dev.bus_id, BUS_ID_SIZE,
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 22ed96c4b7bd..a0cee86464ca 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -27,12 +27,10 @@ static void mtd_notify_add(struct mtd_info* mtd)
27 if (!mtd) 27 if (!mtd)
28 return; 28 return;
29 29
30 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), 30 device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), "mtd%d", mtd->index);
31 NULL, "mtd%d", mtd->index);
32 31
33 class_device_create(mtd_class, NULL, 32 device_create(mtd_class, NULL,
34 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1), 33 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1), "mtd%dro", mtd->index);
35 NULL, "mtd%dro", mtd->index);
36} 34}
37 35
38static void mtd_notify_remove(struct mtd_info* mtd) 36static void mtd_notify_remove(struct mtd_info* mtd)
@@ -40,8 +38,8 @@ static void mtd_notify_remove(struct mtd_info* mtd)
40 if (!mtd) 38 if (!mtd)
41 return; 39 return;
42 40
43 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2)); 41 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
44 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1)); 42 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
45} 43}
46 44
47static struct mtd_notifier notifier = { 45static struct mtd_notifier notifier = {
diff --git a/drivers/net/ibmveth.c b/drivers/net/ibmveth.c
index 7d7758f3ad8c..57772bebff56 100644
--- a/drivers/net/ibmveth.c
+++ b/drivers/net/ibmveth.c
@@ -1179,13 +1179,15 @@ static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_
1179 1179
1180 for(i = 0; i<IbmVethNumBufferPools; i++) { 1180 for(i = 0; i<IbmVethNumBufferPools; i++) {
1181 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj; 1181 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1182 int error;
1183
1182 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i, 1184 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1183 pool_count[i], pool_size[i], 1185 pool_count[i], pool_size[i],
1184 pool_active[i]); 1186 pool_active[i]);
1185 kobj->parent = &dev->dev.kobj; 1187 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1186 kobject_set_name(kobj, "pool%d", i); 1188 &dev->dev.kobj, "pool%d", i);
1187 kobj->ktype = &ktype_veth_pool; 1189 if (!error)
1188 kobject_register(kobj); 1190 kobject_uevent(kobj, KOBJ_ADD);
1189 } 1191 }
1190 1192
1191 ibmveth_debug_printk("adapter @ 0x%p\n", adapter); 1193 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
@@ -1234,7 +1236,7 @@ static int __devexit ibmveth_remove(struct vio_dev *dev)
1234 int i; 1236 int i;
1235 1237
1236 for(i = 0; i<IbmVethNumBufferPools; i++) 1238 for(i = 0; i<IbmVethNumBufferPools; i++)
1237 kobject_unregister(&adapter->rx_buff_pool[i].kobj); 1239 kobject_put(&adapter->rx_buff_pool[i].kobj);
1238 1240
1239 unregister_netdev(netdev); 1241 unregister_netdev(netdev);
1240 1242
diff --git a/drivers/net/iseries_veth.c b/drivers/net/iseries_veth.c
index 97bd9dc2e52e..419861cbc65e 100644
--- a/drivers/net/iseries_veth.c
+++ b/drivers/net/iseries_veth.c
@@ -815,7 +815,7 @@ static int veth_init_connection(u8 rlp)
815{ 815{
816 struct veth_lpar_connection *cnx; 816 struct veth_lpar_connection *cnx;
817 struct veth_msg *msgs; 817 struct veth_msg *msgs;
818 int i, rc; 818 int i;
819 819
820 if ( (rlp == this_lp) 820 if ( (rlp == this_lp)
821 || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) ) 821 || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
@@ -844,11 +844,7 @@ static int veth_init_connection(u8 rlp)
844 844
845 /* This gets us 1 reference, which is held on behalf of the driver 845 /* This gets us 1 reference, which is held on behalf of the driver
846 * infrastructure. It's released at module unload. */ 846 * infrastructure. It's released at module unload. */
847 kobject_init(&cnx->kobject); 847 kobject_init(&cnx->kobject, &veth_lpar_connection_ktype);
848 cnx->kobject.ktype = &veth_lpar_connection_ktype;
849 rc = kobject_set_name(&cnx->kobject, "cnx%.2d", rlp);
850 if (rc != 0)
851 return rc;
852 848
853 msgs = kcalloc(VETH_NUMBUFFERS, sizeof(struct veth_msg), GFP_KERNEL); 849 msgs = kcalloc(VETH_NUMBUFFERS, sizeof(struct veth_msg), GFP_KERNEL);
854 if (! msgs) { 850 if (! msgs) {
@@ -1087,11 +1083,8 @@ static struct net_device * __init veth_probe_one(int vlan,
1087 return NULL; 1083 return NULL;
1088 } 1084 }
1089 1085
1090 kobject_init(&port->kobject); 1086 kobject_init(&port->kobject, &veth_port_ktype);
1091 port->kobject.parent = &dev->dev.kobj; 1087 if (0 != kobject_add(&port->kobject, &dev->dev.kobj, "veth_port"))
1092 port->kobject.ktype = &veth_port_ktype;
1093 kobject_set_name(&port->kobject, "veth_port");
1094 if (0 != kobject_add(&port->kobject))
1095 veth_error("Failed adding port for %s to sysfs.\n", dev->name); 1088 veth_error("Failed adding port for %s to sysfs.\n", dev->name);
1096 1089
1097 veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n", 1090 veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
@@ -1711,9 +1704,9 @@ static int __init veth_module_init(void)
1711 continue; 1704 continue;
1712 1705
1713 kobj = &veth_cnx[i]->kobject; 1706 kobj = &veth_cnx[i]->kobject;
1714 kobj->parent = &veth_driver.driver.kobj;
1715 /* If the add failes, complain but otherwise continue */ 1707 /* If the add failes, complain but otherwise continue */
1716 if (0 != kobject_add(kobj)) 1708 if (0 != driver_add_kobj(&veth_driver.driver, kobj,
1709 "cnx%.2d", veth_cnx[i]->remote_lp))
1717 veth_error("cnx %d: Failed adding to sysfs.\n", i); 1710 veth_error("cnx %d: Failed adding to sysfs.\n", i);
1718 } 1711 }
1719 1712
diff --git a/drivers/net/wan/cosa.c b/drivers/net/wan/cosa.c
index ff37bf437a99..1d706eae3052 100644
--- a/drivers/net/wan/cosa.c
+++ b/drivers/net/wan/cosa.c
@@ -395,8 +395,7 @@ static int __init cosa_init(void)
395 goto out_chrdev; 395 goto out_chrdev;
396 } 396 }
397 for (i=0; i<nr_cards; i++) { 397 for (i=0; i<nr_cards; i++) {
398 class_device_create(cosa_class, NULL, MKDEV(cosa_major, i), 398 device_create(cosa_class, NULL, MKDEV(cosa_major, i), "cosa%d", i);
399 NULL, "cosa%d", i);
400 } 399 }
401 err = 0; 400 err = 0;
402 goto out; 401 goto out;
@@ -415,7 +414,7 @@ static void __exit cosa_exit(void)
415 printk(KERN_INFO "Unloading the cosa module\n"); 414 printk(KERN_INFO "Unloading the cosa module\n");
416 415
417 for (i=0; i<nr_cards; i++) 416 for (i=0; i<nr_cards; i++)
418 class_device_destroy(cosa_class, MKDEV(cosa_major, i)); 417 device_destroy(cosa_class, MKDEV(cosa_major, i));
419 class_destroy(cosa_class); 418 class_destroy(cosa_class);
420 for (cosa=cosa_cards; nr_cards--; cosa++) { 419 for (cosa=cosa_cards; nr_cards--; cosa++) {
421 /* Clean up the per-channel data */ 420 /* Clean up the per-channel data */
diff --git a/drivers/parisc/pdc_stable.c b/drivers/parisc/pdc_stable.c
index ebb09e98d215..de34aa9d3136 100644
--- a/drivers/parisc/pdc_stable.c
+++ b/drivers/parisc/pdc_stable.c
@@ -120,7 +120,7 @@ struct pdcspath_entry pdcspath_entry_##_name = { \
120}; 120};
121 121
122#define PDCS_ATTR(_name, _mode, _show, _store) \ 122#define PDCS_ATTR(_name, _mode, _show, _store) \
123struct subsys_attribute pdcs_attr_##_name = { \ 123struct kobj_attribute pdcs_attr_##_name = { \
124 .attr = {.name = __stringify(_name), .mode = _mode}, \ 124 .attr = {.name = __stringify(_name), .mode = _mode}, \
125 .show = _show, \ 125 .show = _show, \
126 .store = _store, \ 126 .store = _store, \
@@ -523,15 +523,15 @@ static struct pdcspath_entry *pdcspath_entries[] = {
523 523
524/** 524/**
525 * pdcs_size_read - Stable Storage size output. 525 * pdcs_size_read - Stable Storage size output.
526 * @kset: An allocated and populated struct kset. We don't use it tho.
527 * @buf: The output buffer to write to. 526 * @buf: The output buffer to write to.
528 */ 527 */
529static ssize_t 528static ssize_t pdcs_size_read(struct kobject *kobj,
530pdcs_size_read(struct kset *kset, char *buf) 529 struct kobj_attribute *attr,
530 char *buf)
531{ 531{
532 char *out = buf; 532 char *out = buf;
533 533
534 if (!kset || !buf) 534 if (!buf)
535 return -EINVAL; 535 return -EINVAL;
536 536
537 /* show the size of the stable storage */ 537 /* show the size of the stable storage */
@@ -542,17 +542,17 @@ pdcs_size_read(struct kset *kset, char *buf)
542 542
543/** 543/**
544 * pdcs_auto_read - Stable Storage autoboot/search flag output. 544 * pdcs_auto_read - Stable Storage autoboot/search flag output.
545 * @kset: An allocated and populated struct kset. We don't use it tho.
546 * @buf: The output buffer to write to. 545 * @buf: The output buffer to write to.
547 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag 546 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
548 */ 547 */
549static ssize_t 548static ssize_t pdcs_auto_read(struct kobject *kobj,
550pdcs_auto_read(struct kset *kset, char *buf, int knob) 549 struct kobj_attribute *attr,
550 char *buf, int knob)
551{ 551{
552 char *out = buf; 552 char *out = buf;
553 struct pdcspath_entry *pathentry; 553 struct pdcspath_entry *pathentry;
554 554
555 if (!kset || !buf) 555 if (!buf)
556 return -EINVAL; 556 return -EINVAL;
557 557
558 /* Current flags are stored in primary boot path entry */ 558 /* Current flags are stored in primary boot path entry */
@@ -568,40 +568,37 @@ pdcs_auto_read(struct kset *kset, char *buf, int knob)
568 568
569/** 569/**
570 * pdcs_autoboot_read - Stable Storage autoboot flag output. 570 * pdcs_autoboot_read - Stable Storage autoboot flag output.
571 * @kset: An allocated and populated struct kset. We don't use it tho.
572 * @buf: The output buffer to write to. 571 * @buf: The output buffer to write to.
573 */ 572 */
574static inline ssize_t 573static ssize_t pdcs_autoboot_read(struct kobject *kobj,
575pdcs_autoboot_read(struct kset *kset, char *buf) 574 struct kobj_attribute *attr, char *buf)
576{ 575{
577 return pdcs_auto_read(kset, buf, PF_AUTOBOOT); 576 return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
578} 577}
579 578
580/** 579/**
581 * pdcs_autosearch_read - Stable Storage autoboot flag output. 580 * pdcs_autosearch_read - Stable Storage autoboot flag output.
582 * @kset: An allocated and populated struct kset. We don't use it tho.
583 * @buf: The output buffer to write to. 581 * @buf: The output buffer to write to.
584 */ 582 */
585static inline ssize_t 583static ssize_t pdcs_autosearch_read(struct kobject *kobj,
586pdcs_autosearch_read(struct kset *kset, char *buf) 584 struct kobj_attribute *attr, char *buf)
587{ 585{
588 return pdcs_auto_read(kset, buf, PF_AUTOSEARCH); 586 return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
589} 587}
590 588
591/** 589/**
592 * pdcs_timer_read - Stable Storage timer count output (in seconds). 590 * pdcs_timer_read - Stable Storage timer count output (in seconds).
593 * @kset: An allocated and populated struct kset. We don't use it tho.
594 * @buf: The output buffer to write to. 591 * @buf: The output buffer to write to.
595 * 592 *
596 * The value of the timer field correponds to a number of seconds in powers of 2. 593 * The value of the timer field correponds to a number of seconds in powers of 2.
597 */ 594 */
598static ssize_t 595static ssize_t pdcs_timer_read(struct kobject *kobj,
599pdcs_timer_read(struct kset *kset, char *buf) 596 struct kobj_attribute *attr, char *buf)
600{ 597{
601 char *out = buf; 598 char *out = buf;
602 struct pdcspath_entry *pathentry; 599 struct pdcspath_entry *pathentry;
603 600
604 if (!kset || !buf) 601 if (!buf)
605 return -EINVAL; 602 return -EINVAL;
606 603
607 /* Current flags are stored in primary boot path entry */ 604 /* Current flags are stored in primary boot path entry */
@@ -618,15 +615,14 @@ pdcs_timer_read(struct kset *kset, char *buf)
618 615
619/** 616/**
620 * pdcs_osid_read - Stable Storage OS ID register output. 617 * pdcs_osid_read - Stable Storage OS ID register output.
621 * @kset: An allocated and populated struct kset. We don't use it tho.
622 * @buf: The output buffer to write to. 618 * @buf: The output buffer to write to.
623 */ 619 */
624static ssize_t 620static ssize_t pdcs_osid_read(struct kobject *kobj,
625pdcs_osid_read(struct kset *kset, char *buf) 621 struct kobj_attribute *attr, char *buf)
626{ 622{
627 char *out = buf; 623 char *out = buf;
628 624
629 if (!kset || !buf) 625 if (!buf)
630 return -EINVAL; 626 return -EINVAL;
631 627
632 out += sprintf(out, "%s dependent data (0x%.4x)\n", 628 out += sprintf(out, "%s dependent data (0x%.4x)\n",
@@ -637,18 +633,17 @@ pdcs_osid_read(struct kset *kset, char *buf)
637 633
638/** 634/**
639 * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output. 635 * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
640 * @kset: An allocated and populated struct kset. We don't use it tho.
641 * @buf: The output buffer to write to. 636 * @buf: The output buffer to write to.
642 * 637 *
643 * This can hold 16 bytes of OS-Dependent data. 638 * This can hold 16 bytes of OS-Dependent data.
644 */ 639 */
645static ssize_t 640static ssize_t pdcs_osdep1_read(struct kobject *kobj,
646pdcs_osdep1_read(struct kset *kset, char *buf) 641 struct kobj_attribute *attr, char *buf)
647{ 642{
648 char *out = buf; 643 char *out = buf;
649 u32 result[4]; 644 u32 result[4];
650 645
651 if (!kset || !buf) 646 if (!buf)
652 return -EINVAL; 647 return -EINVAL;
653 648
654 if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK) 649 if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
@@ -664,18 +659,17 @@ pdcs_osdep1_read(struct kset *kset, char *buf)
664 659
665/** 660/**
666 * pdcs_diagnostic_read - Stable Storage Diagnostic register output. 661 * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
667 * @kset: An allocated and populated struct kset. We don't use it tho.
668 * @buf: The output buffer to write to. 662 * @buf: The output buffer to write to.
669 * 663 *
670 * I have NFC how to interpret the content of that register ;-). 664 * I have NFC how to interpret the content of that register ;-).
671 */ 665 */
672static ssize_t 666static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
673pdcs_diagnostic_read(struct kset *kset, char *buf) 667 struct kobj_attribute *attr, char *buf)
674{ 668{
675 char *out = buf; 669 char *out = buf;
676 u32 result; 670 u32 result;
677 671
678 if (!kset || !buf) 672 if (!buf)
679 return -EINVAL; 673 return -EINVAL;
680 674
681 /* get diagnostic */ 675 /* get diagnostic */
@@ -689,18 +683,17 @@ pdcs_diagnostic_read(struct kset *kset, char *buf)
689 683
690/** 684/**
691 * pdcs_fastsize_read - Stable Storage FastSize register output. 685 * pdcs_fastsize_read - Stable Storage FastSize register output.
692 * @kset: An allocated and populated struct kset. We don't use it tho.
693 * @buf: The output buffer to write to. 686 * @buf: The output buffer to write to.
694 * 687 *
695 * This register holds the amount of system RAM to be tested during boot sequence. 688 * This register holds the amount of system RAM to be tested during boot sequence.
696 */ 689 */
697static ssize_t 690static ssize_t pdcs_fastsize_read(struct kobject *kobj,
698pdcs_fastsize_read(struct kset *kset, char *buf) 691 struct kobj_attribute *attr, char *buf)
699{ 692{
700 char *out = buf; 693 char *out = buf;
701 u32 result; 694 u32 result;
702 695
703 if (!kset || !buf) 696 if (!buf)
704 return -EINVAL; 697 return -EINVAL;
705 698
706 /* get fast-size */ 699 /* get fast-size */
@@ -718,13 +711,12 @@ pdcs_fastsize_read(struct kset *kset, char *buf)
718 711
719/** 712/**
720 * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output. 713 * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
721 * @kset: An allocated and populated struct kset. We don't use it tho.
722 * @buf: The output buffer to write to. 714 * @buf: The output buffer to write to.
723 * 715 *
724 * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available. 716 * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
725 */ 717 */
726static ssize_t 718static ssize_t pdcs_osdep2_read(struct kobject *kobj,
727pdcs_osdep2_read(struct kset *kset, char *buf) 719 struct kobj_attribute *attr, char *buf)
728{ 720{
729 char *out = buf; 721 char *out = buf;
730 unsigned long size; 722 unsigned long size;
@@ -736,7 +728,7 @@ pdcs_osdep2_read(struct kset *kset, char *buf)
736 728
737 size = pdcs_size - 224; 729 size = pdcs_size - 224;
738 730
739 if (!kset || !buf) 731 if (!buf)
740 return -EINVAL; 732 return -EINVAL;
741 733
742 for (i=0; i<size; i+=4) { 734 for (i=0; i<size; i+=4) {
@@ -751,7 +743,6 @@ pdcs_osdep2_read(struct kset *kset, char *buf)
751 743
752/** 744/**
753 * pdcs_auto_write - This function handles autoboot/search flag modifying. 745 * pdcs_auto_write - This function handles autoboot/search flag modifying.
754 * @kset: An allocated and populated struct kset. We don't use it tho.
755 * @buf: The input buffer to read from. 746 * @buf: The input buffer to read from.
756 * @count: The number of bytes to be read. 747 * @count: The number of bytes to be read.
757 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag 748 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
@@ -760,8 +751,9 @@ pdcs_osdep2_read(struct kset *kset, char *buf)
760 * We expect a precise syntax: 751 * We expect a precise syntax:
761 * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On 752 * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
762 */ 753 */
763static ssize_t 754static ssize_t pdcs_auto_write(struct kobject *kobj,
764pdcs_auto_write(struct kset *kset, const char *buf, size_t count, int knob) 755 struct kobj_attribute *attr, const char *buf,
756 size_t count, int knob)
765{ 757{
766 struct pdcspath_entry *pathentry; 758 struct pdcspath_entry *pathentry;
767 unsigned char flags; 759 unsigned char flags;
@@ -771,7 +763,7 @@ pdcs_auto_write(struct kset *kset, const char *buf, size_t count, int knob)
771 if (!capable(CAP_SYS_ADMIN)) 763 if (!capable(CAP_SYS_ADMIN))
772 return -EACCES; 764 return -EACCES;
773 765
774 if (!kset || !buf || !count) 766 if (!buf || !count)
775 return -EINVAL; 767 return -EINVAL;
776 768
777 /* We'll use a local copy of buf */ 769 /* We'll use a local copy of buf */
@@ -826,7 +818,6 @@ parse_error:
826 818
827/** 819/**
828 * pdcs_autoboot_write - This function handles autoboot flag modifying. 820 * pdcs_autoboot_write - This function handles autoboot flag modifying.
829 * @kset: An allocated and populated struct kset. We don't use it tho.
830 * @buf: The input buffer to read from. 821 * @buf: The input buffer to read from.
831 * @count: The number of bytes to be read. 822 * @count: The number of bytes to be read.
832 * 823 *
@@ -834,15 +825,15 @@ parse_error:
834 * We expect a precise syntax: 825 * We expect a precise syntax:
835 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On 826 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
836 */ 827 */
837static inline ssize_t 828static ssize_t pdcs_autoboot_write(struct kobject *kobj,
838pdcs_autoboot_write(struct kset *kset, const char *buf, size_t count) 829 struct kobj_attribute *attr,
830 const char *buf, size_t count)
839{ 831{
840 return pdcs_auto_write(kset, buf, count, PF_AUTOBOOT); 832 return pdcs_auto_write(kset, attr, buf, count, PF_AUTOBOOT);
841} 833}
842 834
843/** 835/**
844 * pdcs_autosearch_write - This function handles autosearch flag modifying. 836 * pdcs_autosearch_write - This function handles autosearch flag modifying.
845 * @kset: An allocated and populated struct kset. We don't use it tho.
846 * @buf: The input buffer to read from. 837 * @buf: The input buffer to read from.
847 * @count: The number of bytes to be read. 838 * @count: The number of bytes to be read.
848 * 839 *
@@ -850,15 +841,15 @@ pdcs_autoboot_write(struct kset *kset, const char *buf, size_t count)
850 * We expect a precise syntax: 841 * We expect a precise syntax:
851 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On 842 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
852 */ 843 */
853static inline ssize_t 844static ssize_t pdcs_autosearch_write(struct kobject *kobj,
854pdcs_autosearch_write(struct kset *kset, const char *buf, size_t count) 845 struct kobj_attribute *attr,
846 const char *buf, size_t count)
855{ 847{
856 return pdcs_auto_write(kset, buf, count, PF_AUTOSEARCH); 848 return pdcs_auto_write(kset, attr, buf, count, PF_AUTOSEARCH);
857} 849}
858 850
859/** 851/**
860 * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input. 852 * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
861 * @kset: An allocated and populated struct kset. We don't use it tho.
862 * @buf: The input buffer to read from. 853 * @buf: The input buffer to read from.
863 * @count: The number of bytes to be read. 854 * @count: The number of bytes to be read.
864 * 855 *
@@ -866,15 +857,16 @@ pdcs_autosearch_write(struct kset *kset, const char *buf, size_t count)
866 * write approach. It's up to userspace to deal with it when constructing 857 * write approach. It's up to userspace to deal with it when constructing
867 * its input buffer. 858 * its input buffer.
868 */ 859 */
869static ssize_t 860static ssize_t pdcs_osdep1_write(struct kobject *kobj,
870pdcs_osdep1_write(struct kset *kset, const char *buf, size_t count) 861 struct kobj_attribute *attr,
862 const char *buf, size_t count)
871{ 863{
872 u8 in[16]; 864 u8 in[16];
873 865
874 if (!capable(CAP_SYS_ADMIN)) 866 if (!capable(CAP_SYS_ADMIN))
875 return -EACCES; 867 return -EACCES;
876 868
877 if (!kset || !buf || !count) 869 if (!buf || !count)
878 return -EINVAL; 870 return -EINVAL;
879 871
880 if (unlikely(pdcs_osid != OS_ID_LINUX)) 872 if (unlikely(pdcs_osid != OS_ID_LINUX))
@@ -895,7 +887,6 @@ pdcs_osdep1_write(struct kset *kset, const char *buf, size_t count)
895 887
896/** 888/**
897 * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input. 889 * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
898 * @kset: An allocated and populated struct kset. We don't use it tho.
899 * @buf: The input buffer to read from. 890 * @buf: The input buffer to read from.
900 * @count: The number of bytes to be read. 891 * @count: The number of bytes to be read.
901 * 892 *
@@ -903,8 +894,9 @@ pdcs_osdep1_write(struct kset *kset, const char *buf, size_t count)
903 * byte-by-byte write approach. It's up to userspace to deal with it when 894 * byte-by-byte write approach. It's up to userspace to deal with it when
904 * constructing its input buffer. 895 * constructing its input buffer.
905 */ 896 */
906static ssize_t 897static ssize_t pdcs_osdep2_write(struct kobject *kobj,
907pdcs_osdep2_write(struct kset *kset, const char *buf, size_t count) 898 struct kobj_attribute *attr,
899 const char *buf, size_t count)
908{ 900{
909 unsigned long size; 901 unsigned long size;
910 unsigned short i; 902 unsigned short i;
@@ -913,7 +905,7 @@ pdcs_osdep2_write(struct kset *kset, const char *buf, size_t count)
913 if (!capable(CAP_SYS_ADMIN)) 905 if (!capable(CAP_SYS_ADMIN))
914 return -EACCES; 906 return -EACCES;
915 907
916 if (!kset || !buf || !count) 908 if (!buf || !count)
917 return -EINVAL; 909 return -EINVAL;
918 910
919 if (unlikely(pdcs_size <= 224)) 911 if (unlikely(pdcs_size <= 224))
@@ -951,21 +943,25 @@ static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
951static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL); 943static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
952static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write); 944static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
953 945
954static struct subsys_attribute *pdcs_subsys_attrs[] = { 946static struct attribute *pdcs_subsys_attrs[] = {
955 &pdcs_attr_size, 947 &pdcs_attr_size.attr,
956 &pdcs_attr_autoboot, 948 &pdcs_attr_autoboot.attr,
957 &pdcs_attr_autosearch, 949 &pdcs_attr_autosearch.attr,
958 &pdcs_attr_timer, 950 &pdcs_attr_timer.attr,
959 &pdcs_attr_osid, 951 &pdcs_attr_osid.attr,
960 &pdcs_attr_osdep1, 952 &pdcs_attr_osdep1.attr,
961 &pdcs_attr_diagnostic, 953 &pdcs_attr_diagnostic.attr,
962 &pdcs_attr_fastsize, 954 &pdcs_attr_fastsize.attr,
963 &pdcs_attr_osdep2, 955 &pdcs_attr_osdep2.attr,
964 NULL, 956 NULL,
965}; 957};
966 958
967static decl_subsys(paths, &ktype_pdcspath, NULL); 959static struct attribute_group pdcs_attr_group = {
968static decl_subsys(stable, NULL, NULL); 960 .attrs = pdcs_subsys_attrs,
961};
962
963static struct kobject *stable_kobj;
964static struct kset *paths_kset;
969 965
970/** 966/**
971 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage. 967 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
@@ -995,12 +991,12 @@ pdcs_register_pathentries(void)
995 if (err < 0) 991 if (err < 0)
996 continue; 992 continue;
997 993
998 if ((err = kobject_set_name(&entry->kobj, "%s", entry->name))) 994 entry->kobj.kset = paths_kset;
999 return err; 995 err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
1000 kobj_set_kset_s(entry, paths_subsys); 996 "%s", entry->name);
1001 if ((err = kobject_register(&entry->kobj))) 997 if (err)
1002 return err; 998 return err;
1003 999
1004 /* kobject is now registered */ 1000 /* kobject is now registered */
1005 write_lock(&entry->rw_lock); 1001 write_lock(&entry->rw_lock);
1006 entry->ready = 2; 1002 entry->ready = 2;
@@ -1012,6 +1008,7 @@ pdcs_register_pathentries(void)
1012 } 1008 }
1013 1009
1014 write_unlock(&entry->rw_lock); 1010 write_unlock(&entry->rw_lock);
1011 kobject_uevent(&entry->kobj, KOBJ_ADD);
1015 } 1012 }
1016 1013
1017 return 0; 1014 return 0;
@@ -1029,7 +1026,7 @@ pdcs_unregister_pathentries(void)
1029 for (i = 0; (entry = pdcspath_entries[i]); i++) { 1026 for (i = 0; (entry = pdcspath_entries[i]); i++) {
1030 read_lock(&entry->rw_lock); 1027 read_lock(&entry->rw_lock);
1031 if (entry->ready >= 2) 1028 if (entry->ready >= 2)
1032 kobject_unregister(&entry->kobj); 1029 kobject_put(&entry->kobj);
1033 read_unlock(&entry->rw_lock); 1030 read_unlock(&entry->rw_lock);
1034 } 1031 }
1035} 1032}
@@ -1041,8 +1038,7 @@ pdcs_unregister_pathentries(void)
1041static int __init 1038static int __init
1042pdc_stable_init(void) 1039pdc_stable_init(void)
1043{ 1040{
1044 struct subsys_attribute *attr; 1041 int rc = 0, error = 0;
1045 int i, rc = 0, error = 0;
1046 u32 result; 1042 u32 result;
1047 1043
1048 /* find the size of the stable storage */ 1044 /* find the size of the stable storage */
@@ -1062,21 +1058,24 @@ pdc_stable_init(void)
1062 /* the actual result is 16 bits away */ 1058 /* the actual result is 16 bits away */
1063 pdcs_osid = (u16)(result >> 16); 1059 pdcs_osid = (u16)(result >> 16);
1064 1060
1065 /* For now we'll register the stable subsys within this driver */ 1061 /* For now we'll register the directory at /sys/firmware/stable */
1066 if ((rc = firmware_register(&stable_subsys))) 1062 stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1063 if (!stable_kobj) {
1064 rc = -ENOMEM;
1067 goto fail_firmreg; 1065 goto fail_firmreg;
1066 }
1068 1067
1069 /* Don't forget the root entries */ 1068 /* Don't forget the root entries */
1070 for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++) 1069 error = sysfs_create_group(stable_kobj, pdcs_attr_group);
1071 if (attr->show)
1072 error = subsys_create_file(&stable_subsys, attr);
1073
1074 /* register the paths subsys as a subsystem of stable subsys */
1075 kobj_set_kset_s(&paths_subsys, stable_subsys);
1076 if ((rc = subsystem_register(&paths_subsys)))
1077 goto fail_subsysreg;
1078 1070
1079 /* now we create all "files" for the paths subsys */ 1071 /* register the paths kset as a child of the stable kset */
1072 paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
1073 if (!paths_kset) {
1074 rc = -ENOMEM;
1075 goto fail_ksetreg;
1076 }
1077
1078 /* now we create all "files" for the paths kset */
1080 if ((rc = pdcs_register_pathentries())) 1079 if ((rc = pdcs_register_pathentries()))
1081 goto fail_pdcsreg; 1080 goto fail_pdcsreg;
1082 1081
@@ -1084,10 +1083,10 @@ pdc_stable_init(void)
1084 1083
1085fail_pdcsreg: 1084fail_pdcsreg:
1086 pdcs_unregister_pathentries(); 1085 pdcs_unregister_pathentries();
1087 subsystem_unregister(&paths_subsys); 1086 kset_unregister(paths_kset);
1088 1087
1089fail_subsysreg: 1088fail_ksetreg:
1090 firmware_unregister(&stable_subsys); 1089 kobject_put(stable_kobj);
1091 1090
1092fail_firmreg: 1091fail_firmreg:
1093 printk(KERN_INFO PDCS_PREFIX " bailing out\n"); 1092 printk(KERN_INFO PDCS_PREFIX " bailing out\n");
@@ -1098,9 +1097,8 @@ static void __exit
1098pdc_stable_exit(void) 1097pdc_stable_exit(void)
1099{ 1098{
1100 pdcs_unregister_pathentries(); 1099 pdcs_unregister_pathentries();
1101 subsystem_unregister(&paths_subsys); 1100 kset_unregister(paths_kset);
1102 1101 kobject_put(stable_kobj);
1103 firmware_unregister(&stable_subsys);
1104} 1102}
1105 1103
1106 1104
diff --git a/drivers/pci/hotplug/acpiphp_ibm.c b/drivers/pci/hotplug/acpiphp_ibm.c
index 47d26b65e99a..750ebd7a4c10 100644
--- a/drivers/pci/hotplug/acpiphp_ibm.c
+++ b/drivers/pci/hotplug/acpiphp_ibm.c
@@ -429,7 +429,7 @@ static int __init ibm_acpiphp_init(void)
429 int retval = 0; 429 int retval = 0;
430 acpi_status status; 430 acpi_status status;
431 struct acpi_device *device; 431 struct acpi_device *device;
432 struct kobject *sysdir = &pci_hotplug_slots_subsys.kobj; 432 struct kobject *sysdir = &pci_hotplug_slots_kset->kobj;
433 433
434 dbg("%s\n", __FUNCTION__); 434 dbg("%s\n", __FUNCTION__);
435 435
@@ -476,7 +476,7 @@ init_return:
476static void __exit ibm_acpiphp_exit(void) 476static void __exit ibm_acpiphp_exit(void)
477{ 477{
478 acpi_status status; 478 acpi_status status;
479 struct kobject *sysdir = &pci_hotplug_slots_subsys.kobj; 479 struct kobject *sysdir = &pci_hotplug_slots_kset->kobj;
480 480
481 dbg("%s\n", __FUNCTION__); 481 dbg("%s\n", __FUNCTION__);
482 482
diff --git a/drivers/pci/hotplug/pci_hotplug_core.c b/drivers/pci/hotplug/pci_hotplug_core.c
index 01c351c176ac..47bb0e1ff3fa 100644
--- a/drivers/pci/hotplug/pci_hotplug_core.c
+++ b/drivers/pci/hotplug/pci_hotplug_core.c
@@ -61,7 +61,7 @@ static int debug;
61 61
62static LIST_HEAD(pci_hotplug_slot_list); 62static LIST_HEAD(pci_hotplug_slot_list);
63 63
64struct kset pci_hotplug_slots_subsys; 64struct kset *pci_hotplug_slots_kset;
65 65
66static ssize_t hotplug_slot_attr_show(struct kobject *kobj, 66static ssize_t hotplug_slot_attr_show(struct kobject *kobj,
67 struct attribute *attr, char *buf) 67 struct attribute *attr, char *buf)
@@ -96,8 +96,6 @@ static struct kobj_type hotplug_slot_ktype = {
96 .release = &hotplug_slot_release, 96 .release = &hotplug_slot_release,
97}; 97};
98 98
99decl_subsys_name(pci_hotplug_slots, slots, &hotplug_slot_ktype, NULL);
100
101/* these strings match up with the values in pci_bus_speed */ 99/* these strings match up with the values in pci_bus_speed */
102static char *pci_bus_speed_strings[] = { 100static char *pci_bus_speed_strings[] = {
103 "33 MHz PCI", /* 0x00 */ 101 "33 MHz PCI", /* 0x00 */
@@ -632,18 +630,19 @@ int pci_hp_register (struct hotplug_slot *slot)
632 return -EINVAL; 630 return -EINVAL;
633 } 631 }
634 632
635 kobject_set_name(&slot->kobj, "%s", slot->name);
636 kobj_set_kset_s(slot, pci_hotplug_slots_subsys);
637
638 /* this can fail if we have already registered a slot with the same name */ 633 /* this can fail if we have already registered a slot with the same name */
639 if (kobject_register(&slot->kobj)) { 634 slot->kobj.kset = pci_hotplug_slots_kset;
640 err("Unable to register kobject"); 635 result = kobject_init_and_add(&slot->kobj, &hotplug_slot_ktype, NULL,
636 "%s", slot->name);
637 if (result) {
638 err("Unable to register kobject '%s'", slot->name);
641 return -EINVAL; 639 return -EINVAL;
642 } 640 }
643 641
644 list_add (&slot->slot_list, &pci_hotplug_slot_list); 642 list_add (&slot->slot_list, &pci_hotplug_slot_list);
645 643
646 result = fs_add_slot (slot); 644 result = fs_add_slot (slot);
645 kobject_uevent(&slot->kobj, KOBJ_ADD);
647 dbg ("Added slot %s to the list\n", slot->name); 646 dbg ("Added slot %s to the list\n", slot->name);
648 return result; 647 return result;
649} 648}
@@ -672,7 +671,7 @@ int pci_hp_deregister (struct hotplug_slot *slot)
672 671
673 fs_remove_slot (slot); 672 fs_remove_slot (slot);
674 dbg ("Removed slot %s from the list\n", slot->name); 673 dbg ("Removed slot %s from the list\n", slot->name);
675 kobject_unregister(&slot->kobj); 674 kobject_put(&slot->kobj);
676 return 0; 675 return 0;
677} 676}
678 677
@@ -700,11 +699,15 @@ int __must_check pci_hp_change_slot_info(struct hotplug_slot *slot,
700static int __init pci_hotplug_init (void) 699static int __init pci_hotplug_init (void)
701{ 700{
702 int result; 701 int result;
702 struct kset *pci_bus_kset;
703 703
704 kobj_set_kset_s(&pci_hotplug_slots_subsys, pci_bus_type.subsys); 704 pci_bus_kset = bus_get_kset(&pci_bus_type);
705 result = subsystem_register(&pci_hotplug_slots_subsys); 705
706 if (result) { 706 pci_hotplug_slots_kset = kset_create_and_add("slots", NULL,
707 err("Register subsys with error %d\n", result); 707 &pci_bus_kset->kobj);
708 if (!pci_hotplug_slots_kset) {
709 result = -ENOMEM;
710 err("Register subsys error\n");
708 goto exit; 711 goto exit;
709 } 712 }
710 result = cpci_hotplug_init(debug); 713 result = cpci_hotplug_init(debug);
@@ -715,9 +718,9 @@ static int __init pci_hotplug_init (void)
715 718
716 info (DRIVER_DESC " version: " DRIVER_VERSION "\n"); 719 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
717 goto exit; 720 goto exit;
718 721
719err_subsys: 722err_subsys:
720 subsystem_unregister(&pci_hotplug_slots_subsys); 723 kset_unregister(pci_hotplug_slots_kset);
721exit: 724exit:
722 return result; 725 return result;
723} 726}
@@ -725,7 +728,7 @@ exit:
725static void __exit pci_hotplug_exit (void) 728static void __exit pci_hotplug_exit (void)
726{ 729{
727 cpci_hotplug_exit(); 730 cpci_hotplug_exit();
728 subsystem_unregister(&pci_hotplug_slots_subsys); 731 kset_unregister(pci_hotplug_slots_kset);
729} 732}
730 733
731module_init(pci_hotplug_init); 734module_init(pci_hotplug_init);
@@ -737,7 +740,7 @@ MODULE_LICENSE("GPL");
737module_param(debug, bool, 0644); 740module_param(debug, bool, 0644);
738MODULE_PARM_DESC(debug, "Debugging mode enabled or not"); 741MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
739 742
740EXPORT_SYMBOL_GPL(pci_hotplug_slots_subsys); 743EXPORT_SYMBOL_GPL(pci_hotplug_slots_kset);
741EXPORT_SYMBOL_GPL(pci_hp_register); 744EXPORT_SYMBOL_GPL(pci_hp_register);
742EXPORT_SYMBOL_GPL(pci_hp_deregister); 745EXPORT_SYMBOL_GPL(pci_hp_deregister);
743EXPORT_SYMBOL_GPL(pci_hp_change_slot_info); 746EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);
diff --git a/drivers/pci/hotplug/rpadlpar_sysfs.c b/drivers/pci/hotplug/rpadlpar_sysfs.c
index a080fedf0332..e32148a8fa12 100644
--- a/drivers/pci/hotplug/rpadlpar_sysfs.c
+++ b/drivers/pci/hotplug/rpadlpar_sysfs.c
@@ -23,44 +23,13 @@
23 23
24#define MAX_DRC_NAME_LEN 64 24#define MAX_DRC_NAME_LEN 64
25 25
26/* Store return code of dlpar operation in attribute struct */
27struct dlpar_io_attr {
28 int rc;
29 struct attribute attr;
30 ssize_t (*store)(struct dlpar_io_attr *dlpar_attr, const char *buf,
31 size_t nbytes);
32};
33 26
34/* Common show callback for all attrs, display the return code 27static ssize_t add_slot_store(struct kobject *kobj, struct kobj_attribute *attr,
35 * of the dlpar op */ 28 const char *buf, size_t nbytes)
36static ssize_t
37dlpar_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
38{
39 struct dlpar_io_attr *dlpar_attr = container_of(attr,
40 struct dlpar_io_attr, attr);
41 return sprintf(buf, "%d\n", dlpar_attr->rc);
42}
43
44static ssize_t
45dlpar_attr_store(struct kobject * kobj, struct attribute * attr,
46 const char *buf, size_t nbytes)
47{
48 struct dlpar_io_attr *dlpar_attr = container_of(attr,
49 struct dlpar_io_attr, attr);
50 return dlpar_attr->store ?
51 dlpar_attr->store(dlpar_attr, buf, nbytes) : -EIO;
52}
53
54static struct sysfs_ops dlpar_attr_sysfs_ops = {
55 .show = dlpar_attr_show,
56 .store = dlpar_attr_store,
57};
58
59static ssize_t add_slot_store(struct dlpar_io_attr *dlpar_attr,
60 const char *buf, size_t nbytes)
61{ 29{
62 char drc_name[MAX_DRC_NAME_LEN]; 30 char drc_name[MAX_DRC_NAME_LEN];
63 char *end; 31 char *end;
32 int rc;
64 33
65 if (nbytes >= MAX_DRC_NAME_LEN) 34 if (nbytes >= MAX_DRC_NAME_LEN)
66 return 0; 35 return 0;
@@ -72,15 +41,25 @@ static ssize_t add_slot_store(struct dlpar_io_attr *dlpar_attr,
72 end = &drc_name[nbytes]; 41 end = &drc_name[nbytes];
73 *end = '\0'; 42 *end = '\0';
74 43
75 dlpar_attr->rc = dlpar_add_slot(drc_name); 44 rc = dlpar_add_slot(drc_name);
45 if (rc)
46 return rc;
76 47
77 return nbytes; 48 return nbytes;
78} 49}
79 50
80static ssize_t remove_slot_store(struct dlpar_io_attr *dlpar_attr, 51static ssize_t add_slot_show(struct kobject *kobj,
81 const char *buf, size_t nbytes) 52 struct kobj_attribute *attr, char *buf)
53{
54 return sprintf(buf, "0\n");
55}
56
57static ssize_t remove_slot_store(struct kobject *kobj,
58 struct kobj_attribute *attr,
59 const char *buf, size_t nbytes)
82{ 60{
83 char drc_name[MAX_DRC_NAME_LEN]; 61 char drc_name[MAX_DRC_NAME_LEN];
62 int rc;
84 char *end; 63 char *end;
85 64
86 if (nbytes >= MAX_DRC_NAME_LEN) 65 if (nbytes >= MAX_DRC_NAME_LEN)
@@ -93,22 +72,24 @@ static ssize_t remove_slot_store(struct dlpar_io_attr *dlpar_attr,
93 end = &drc_name[nbytes]; 72 end = &drc_name[nbytes];
94 *end = '\0'; 73 *end = '\0';
95 74
96 dlpar_attr->rc = dlpar_remove_slot(drc_name); 75 rc = dlpar_remove_slot(drc_name);
76 if (rc)
77 return rc;
97 78
98 return nbytes; 79 return nbytes;
99} 80}
100 81
101static struct dlpar_io_attr add_slot_attr = { 82static ssize_t remove_slot_show(struct kobject *kobj,
102 .rc = 0, 83 struct kobj_attribute *attr, char *buf)
103 .attr = { .name = ADD_SLOT_ATTR_NAME, .mode = 0644, }, 84{
104 .store = add_slot_store, 85 return sprintf(buf, "0\n");
105}; 86}
106 87
107static struct dlpar_io_attr remove_slot_attr = { 88static struct kobj_attribute add_slot_attr =
108 .rc = 0, 89 __ATTR(ADD_SLOT_ATTR_NAME, 0644, add_slot_show, add_slot_store);
109 .attr = { .name = REMOVE_SLOT_ATTR_NAME, .mode = 0644}, 90
110 .store = remove_slot_store, 91static struct kobj_attribute remove_slot_attr =
111}; 92 __ATTR(REMOVE_SLOT_ATTR_NAME, 0644, remove_slot_show, remove_slot_store);
112 93
113static struct attribute *default_attrs[] = { 94static struct attribute *default_attrs[] = {
114 &add_slot_attr.attr, 95 &add_slot_attr.attr,
@@ -116,37 +97,29 @@ static struct attribute *default_attrs[] = {
116 NULL, 97 NULL,
117}; 98};
118 99
119static void dlpar_io_release(struct kobject *kobj) 100static struct attribute_group dlpar_attr_group = {
120{ 101 .attrs = default_attrs,
121 /* noop */
122 return;
123}
124
125struct kobj_type ktype_dlpar_io = {
126 .release = dlpar_io_release,
127 .sysfs_ops = &dlpar_attr_sysfs_ops,
128 .default_attrs = default_attrs,
129}; 102};
130 103
131struct kset dlpar_io_kset = { 104static struct kobject *dlpar_kobj;
132 .kobj = {.ktype = &ktype_dlpar_io,
133 .parent = &pci_hotplug_slots_subsys.kobj},
134 .ktype = &ktype_dlpar_io,
135};
136 105
137int dlpar_sysfs_init(void) 106int dlpar_sysfs_init(void)
138{ 107{
139 kobject_set_name(&dlpar_io_kset.kobj, DLPAR_KOBJ_NAME); 108 int error;
140 if (kset_register(&dlpar_io_kset)) { 109
141 printk(KERN_ERR "rpadlpar_io: cannot register kset for %s\n", 110 dlpar_kobj = kobject_create_and_add(DLPAR_KOBJ_NAME,
142 kobject_name(&dlpar_io_kset.kobj)); 111 &pci_hotplug_slots_kset->kobj);
112 if (!dlpar_kobj)
143 return -EINVAL; 113 return -EINVAL;
144 }
145 114
146 return 0; 115 error = sysfs_create_group(dlpar_kobj, &dlpar_attr_group);
116 if (error)
117 kobject_put(dlpar_kobj);
118 return error;
147} 119}
148 120
149void dlpar_sysfs_exit(void) 121void dlpar_sysfs_exit(void)
150{ 122{
151 kset_unregister(&dlpar_io_kset); 123 sysfs_remove_group(dlpar_kobj, &dlpar_attr_group);
124 kobject_put(dlpar_kobj);
152} 125}
diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c
index 6d1a21611818..c4fa35d1dd77 100644
--- a/drivers/pci/pci-driver.c
+++ b/drivers/pci/pci-driver.c
@@ -1,6 +1,11 @@
1/* 1/*
2 * drivers/pci/pci-driver.c 2 * drivers/pci/pci-driver.c
3 * 3 *
4 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2007 Novell Inc.
6 *
7 * Released under the GPL v2 only.
8 *
4 */ 9 */
5 10
6#include <linux/pci.h> 11#include <linux/pci.h>
@@ -96,17 +101,21 @@ pci_create_newid_file(struct pci_driver *drv)
96{ 101{
97 int error = 0; 102 int error = 0;
98 if (drv->probe != NULL) 103 if (drv->probe != NULL)
99 error = sysfs_create_file(&drv->driver.kobj, 104 error = driver_create_file(&drv->driver, &driver_attr_new_id);
100 &driver_attr_new_id.attr);
101 return error; 105 return error;
102} 106}
103 107
108static void pci_remove_newid_file(struct pci_driver *drv)
109{
110 driver_remove_file(&drv->driver, &driver_attr_new_id);
111}
104#else /* !CONFIG_HOTPLUG */ 112#else /* !CONFIG_HOTPLUG */
105static inline void pci_free_dynids(struct pci_driver *drv) {} 113static inline void pci_free_dynids(struct pci_driver *drv) {}
106static inline int pci_create_newid_file(struct pci_driver *drv) 114static inline int pci_create_newid_file(struct pci_driver *drv)
107{ 115{
108 return 0; 116 return 0;
109} 117}
118static inline void pci_remove_newid_file(struct pci_driver *drv) {}
110#endif 119#endif
111 120
112/** 121/**
@@ -352,50 +361,6 @@ static void pci_device_shutdown(struct device *dev)
352 drv->shutdown(pci_dev); 361 drv->shutdown(pci_dev);
353} 362}
354 363
355#define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
356#define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
357
358static ssize_t
359pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
360{
361 struct device_driver *driver = kobj_to_pci_driver(kobj);
362 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
363 ssize_t ret;
364
365 if (!get_driver(driver))
366 return -ENODEV;
367
368 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
369
370 put_driver(driver);
371 return ret;
372}
373
374static ssize_t
375pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
376 const char *buf, size_t count)
377{
378 struct device_driver *driver = kobj_to_pci_driver(kobj);
379 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
380 ssize_t ret;
381
382 if (!get_driver(driver))
383 return -ENODEV;
384
385 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
386
387 put_driver(driver);
388 return ret;
389}
390
391static struct sysfs_ops pci_driver_sysfs_ops = {
392 .show = pci_driver_attr_show,
393 .store = pci_driver_attr_store,
394};
395static struct kobj_type pci_driver_kobj_type = {
396 .sysfs_ops = &pci_driver_sysfs_ops,
397};
398
399/** 364/**
400 * __pci_register_driver - register a new pci driver 365 * __pci_register_driver - register a new pci driver
401 * @drv: the driver structure to register 366 * @drv: the driver structure to register
@@ -417,7 +382,6 @@ int __pci_register_driver(struct pci_driver *drv, struct module *owner,
417 drv->driver.bus = &pci_bus_type; 382 drv->driver.bus = &pci_bus_type;
418 drv->driver.owner = owner; 383 drv->driver.owner = owner;
419 drv->driver.mod_name = mod_name; 384 drv->driver.mod_name = mod_name;
420 drv->driver.kobj.ktype = &pci_driver_kobj_type;
421 385
422 spin_lock_init(&drv->dynids.lock); 386 spin_lock_init(&drv->dynids.lock);
423 INIT_LIST_HEAD(&drv->dynids.list); 387 INIT_LIST_HEAD(&drv->dynids.list);
@@ -447,6 +411,7 @@ int __pci_register_driver(struct pci_driver *drv, struct module *owner,
447void 411void
448pci_unregister_driver(struct pci_driver *drv) 412pci_unregister_driver(struct pci_driver *drv)
449{ 413{
414 pci_remove_newid_file(drv);
450 driver_unregister(&drv->driver); 415 driver_unregister(&drv->driver);
451 pci_free_dynids(drv); 416 pci_free_dynids(drv);
452} 417}
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index c5ca3134513a..5fd585293e79 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -1210,16 +1210,19 @@ static void __init pci_sort_breadthfirst_klist(void)
1210 struct klist_node *n; 1210 struct klist_node *n;
1211 struct device *dev; 1211 struct device *dev;
1212 struct pci_dev *pdev; 1212 struct pci_dev *pdev;
1213 struct klist *device_klist;
1213 1214
1214 spin_lock(&pci_bus_type.klist_devices.k_lock); 1215 device_klist = bus_get_device_klist(&pci_bus_type);
1215 list_for_each_safe(pos, tmp, &pci_bus_type.klist_devices.k_list) { 1216
1217 spin_lock(&device_klist->k_lock);
1218 list_for_each_safe(pos, tmp, &device_klist->k_list) {
1216 n = container_of(pos, struct klist_node, n_node); 1219 n = container_of(pos, struct klist_node, n_node);
1217 dev = container_of(n, struct device, knode_bus); 1220 dev = container_of(n, struct device, knode_bus);
1218 pdev = to_pci_dev(dev); 1221 pdev = to_pci_dev(dev);
1219 pci_insertion_sort_klist(pdev, &sorted_devices); 1222 pci_insertion_sort_klist(pdev, &sorted_devices);
1220 } 1223 }
1221 list_splice(&sorted_devices, &pci_bus_type.klist_devices.k_list); 1224 list_splice(&sorted_devices, &device_klist->k_list);
1222 spin_unlock(&pci_bus_type.klist_devices.k_lock); 1225 spin_unlock(&device_klist->k_lock);
1223} 1226}
1224 1227
1225static void __init pci_insertion_sort_devices(struct pci_dev *a, struct list_head *list) 1228static void __init pci_insertion_sort_devices(struct pci_dev *a, struct list_head *list)
diff --git a/drivers/pcmcia/ds.c b/drivers/pcmcia/ds.c
index 5cf89a91da1e..15c18f5246d6 100644
--- a/drivers/pcmcia/ds.c
+++ b/drivers/pcmcia/ds.c
@@ -312,8 +312,7 @@ pcmcia_create_newid_file(struct pcmcia_driver *drv)
312{ 312{
313 int error = 0; 313 int error = 0;
314 if (drv->probe != NULL) 314 if (drv->probe != NULL)
315 error = sysfs_create_file(&drv->drv.kobj, 315 error = driver_create_file(&drv->drv, &driver_attr_new_id);
316 &driver_attr_new_id.attr);
317 return error; 316 return error;
318} 317}
319 318
diff --git a/drivers/power/apm_power.c b/drivers/power/apm_power.c
index bbf3ee10da04..7e29b90a4f63 100644
--- a/drivers/power/apm_power.c
+++ b/drivers/power/apm_power.c
@@ -13,6 +13,7 @@
13#include <linux/power_supply.h> 13#include <linux/power_supply.h>
14#include <linux/apm-emulation.h> 14#include <linux/apm-emulation.h>
15 15
16static DEFINE_MUTEX(apm_mutex);
16#define PSY_PROP(psy, prop, val) psy->get_property(psy, \ 17#define PSY_PROP(psy, prop, val) psy->get_property(psy, \
17 POWER_SUPPLY_PROP_##prop, val) 18 POWER_SUPPLY_PROP_##prop, val)
18 19
@@ -23,67 +24,86 @@
23 24
24static struct power_supply *main_battery; 25static struct power_supply *main_battery;
25 26
26static void find_main_battery(void) 27struct find_bat_param {
27{ 28 struct power_supply *main;
28 struct device *dev; 29 struct power_supply *bat;
29 struct power_supply *bat = NULL; 30 struct power_supply *max_charge_bat;
30 struct power_supply *max_charge_bat = NULL; 31 struct power_supply *max_energy_bat;
31 struct power_supply *max_energy_bat = NULL;
32 union power_supply_propval full; 32 union power_supply_propval full;
33 int max_charge = 0; 33 int max_charge;
34 int max_energy = 0; 34 int max_energy;
35};
35 36
36 main_battery = NULL; 37static int __find_main_battery(struct device *dev, void *data)
38{
39 struct find_bat_param *bp = (struct find_bat_param *)data;
37 40
38 list_for_each_entry(dev, &power_supply_class->devices, node) { 41 bp->bat = dev_get_drvdata(dev);
39 bat = dev_get_drvdata(dev);
40 42
41 if (bat->use_for_apm) { 43 if (bp->bat->use_for_apm) {
42 /* nice, we explicitly asked to report this battery. */ 44 /* nice, we explicitly asked to report this battery. */
43 main_battery = bat; 45 bp->main = bp->bat;
44 return; 46 return 1;
45 } 47 }
46 48
47 if (!PSY_PROP(bat, CHARGE_FULL_DESIGN, &full) || 49 if (!PSY_PROP(bp->bat, CHARGE_FULL_DESIGN, &bp->full) ||
48 !PSY_PROP(bat, CHARGE_FULL, &full)) { 50 !PSY_PROP(bp->bat, CHARGE_FULL, &bp->full)) {
49 if (full.intval > max_charge) { 51 if (bp->full.intval > bp->max_charge) {
50 max_charge_bat = bat; 52 bp->max_charge_bat = bp->bat;
51 max_charge = full.intval; 53 bp->max_charge = bp->full.intval;
52 } 54 }
53 } else if (!PSY_PROP(bat, ENERGY_FULL_DESIGN, &full) || 55 } else if (!PSY_PROP(bp->bat, ENERGY_FULL_DESIGN, &bp->full) ||
54 !PSY_PROP(bat, ENERGY_FULL, &full)) { 56 !PSY_PROP(bp->bat, ENERGY_FULL, &bp->full)) {
55 if (full.intval > max_energy) { 57 if (bp->full.intval > bp->max_energy) {
56 max_energy_bat = bat; 58 bp->max_energy_bat = bp->bat;
57 max_energy = full.intval; 59 bp->max_energy = bp->full.intval;
58 }
59 } 60 }
60 } 61 }
62 return 0;
63}
64
65static void find_main_battery(void)
66{
67 struct find_bat_param bp;
68 int error;
69
70 memset(&bp, 0, sizeof(struct find_bat_param));
71 main_battery = NULL;
72 bp.main = main_battery;
73
74 error = class_for_each_device(power_supply_class, &bp,
75 __find_main_battery);
76 if (error) {
77 main_battery = bp.main;
78 return;
79 }
61 80
62 if ((max_energy_bat && max_charge_bat) && 81 if ((bp.max_energy_bat && bp.max_charge_bat) &&
63 (max_energy_bat != max_charge_bat)) { 82 (bp.max_energy_bat != bp.max_charge_bat)) {
64 /* try guess battery with more capacity */ 83 /* try guess battery with more capacity */
65 if (!PSY_PROP(max_charge_bat, VOLTAGE_MAX_DESIGN, &full)) { 84 if (!PSY_PROP(bp.max_charge_bat, VOLTAGE_MAX_DESIGN,
66 if (max_energy > max_charge * full.intval) 85 &bp.full)) {
67 main_battery = max_energy_bat; 86 if (bp.max_energy > bp.max_charge * bp.full.intval)
87 main_battery = bp.max_energy_bat;
68 else 88 else
69 main_battery = max_charge_bat; 89 main_battery = bp.max_charge_bat;
70 } else if (!PSY_PROP(max_energy_bat, VOLTAGE_MAX_DESIGN, 90 } else if (!PSY_PROP(bp.max_energy_bat, VOLTAGE_MAX_DESIGN,
71 &full)) { 91 &bp.full)) {
72 if (max_charge > max_energy / full.intval) 92 if (bp.max_charge > bp.max_energy / bp.full.intval)
73 main_battery = max_charge_bat; 93 main_battery = bp.max_charge_bat;
74 else 94 else
75 main_battery = max_energy_bat; 95 main_battery = bp.max_energy_bat;
76 } else { 96 } else {
77 /* give up, choice any */ 97 /* give up, choice any */
78 main_battery = max_energy_bat; 98 main_battery = bp.max_energy_bat;
79 } 99 }
80 } else if (max_charge_bat) { 100 } else if (bp.max_charge_bat) {
81 main_battery = max_charge_bat; 101 main_battery = bp.max_charge_bat;
82 } else if (max_energy_bat) { 102 } else if (bp.max_energy_bat) {
83 main_battery = max_energy_bat; 103 main_battery = bp.max_energy_bat;
84 } else { 104 } else {
85 /* give up, try the last if any */ 105 /* give up, try the last if any */
86 main_battery = bat; 106 main_battery = bp.bat;
87 } 107 }
88} 108}
89 109
@@ -207,10 +227,10 @@ static void apm_battery_apm_get_power_status(struct apm_power_info *info)
207 union power_supply_propval status; 227 union power_supply_propval status;
208 union power_supply_propval capacity, time_to_full, time_to_empty; 228 union power_supply_propval capacity, time_to_full, time_to_empty;
209 229
210 down(&power_supply_class->sem); 230 mutex_lock(&apm_mutex);
211 find_main_battery(); 231 find_main_battery();
212 if (!main_battery) { 232 if (!main_battery) {
213 up(&power_supply_class->sem); 233 mutex_unlock(&apm_mutex);
214 return; 234 return;
215 } 235 }
216 236
@@ -278,7 +298,7 @@ static void apm_battery_apm_get_power_status(struct apm_power_info *info)
278 } 298 }
279 } 299 }
280 300
281 up(&power_supply_class->sem); 301 mutex_unlock(&apm_mutex);
282} 302}
283 303
284static int __init apm_battery_init(void) 304static int __init apm_battery_init(void)
diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index a63b75cf75e2..03d6a38464ef 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -20,28 +20,29 @@
20 20
21struct class *power_supply_class; 21struct class *power_supply_class;
22 22
23static int __power_supply_changed_work(struct device *dev, void *data)
24{
25 struct power_supply *psy = (struct power_supply *)data;
26 struct power_supply *pst = dev_get_drvdata(dev);
27 int i;
28
29 for (i = 0; i < psy->num_supplicants; i++)
30 if (!strcmp(psy->supplied_to[i], pst->name)) {
31 if (pst->external_power_changed)
32 pst->external_power_changed(pst);
33 }
34 return 0;
35}
36
23static void power_supply_changed_work(struct work_struct *work) 37static void power_supply_changed_work(struct work_struct *work)
24{ 38{
25 struct power_supply *psy = container_of(work, struct power_supply, 39 struct power_supply *psy = container_of(work, struct power_supply,
26 changed_work); 40 changed_work);
27 int i;
28 41
29 dev_dbg(psy->dev, "%s\n", __FUNCTION__); 42 dev_dbg(psy->dev, "%s\n", __FUNCTION__);
30 43
31 for (i = 0; i < psy->num_supplicants; i++) { 44 class_for_each_device(power_supply_class, psy,
32 struct device *dev; 45 __power_supply_changed_work);
33
34 down(&power_supply_class->sem);
35 list_for_each_entry(dev, &power_supply_class->devices, node) {
36 struct power_supply *pst = dev_get_drvdata(dev);
37
38 if (!strcmp(psy->supplied_to[i], pst->name)) {
39 if (pst->external_power_changed)
40 pst->external_power_changed(pst);
41 }
42 }
43 up(&power_supply_class->sem);
44 }
45 46
46 power_supply_update_leds(psy); 47 power_supply_update_leds(psy);
47 48
@@ -55,32 +56,35 @@ void power_supply_changed(struct power_supply *psy)
55 schedule_work(&psy->changed_work); 56 schedule_work(&psy->changed_work);
56} 57}
57 58
58int power_supply_am_i_supplied(struct power_supply *psy) 59static int __power_supply_am_i_supplied(struct device *dev, void *data)
59{ 60{
60 union power_supply_propval ret = {0,}; 61 union power_supply_propval ret = {0,};
61 struct device *dev; 62 struct power_supply *psy = (struct power_supply *)data;
62 63 struct power_supply *epsy = dev_get_drvdata(dev);
63 down(&power_supply_class->sem); 64 int i;
64 list_for_each_entry(dev, &power_supply_class->devices, node) { 65
65 struct power_supply *epsy = dev_get_drvdata(dev); 66 for (i = 0; i < epsy->num_supplicants; i++) {
66 int i; 67 if (!strcmp(epsy->supplied_to[i], psy->name)) {
67 68 if (epsy->get_property(epsy,
68 for (i = 0; i < epsy->num_supplicants; i++) { 69 POWER_SUPPLY_PROP_ONLINE, &ret))
69 if (!strcmp(epsy->supplied_to[i], psy->name)) { 70 continue;
70 if (epsy->get_property(epsy, 71 if (ret.intval)
71 POWER_SUPPLY_PROP_ONLINE, &ret)) 72 return ret.intval;
72 continue;
73 if (ret.intval)
74 goto out;
75 }
76 } 73 }
77 } 74 }
78out: 75 return 0;
79 up(&power_supply_class->sem); 76}
77
78int power_supply_am_i_supplied(struct power_supply *psy)
79{
80 int error;
81
82 error = class_for_each_device(power_supply_class, psy,
83 __power_supply_am_i_supplied);
80 84
81 dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, ret.intval); 85 dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, error);
82 86
83 return ret.intval; 87 return error;
84} 88}
85 89
86int power_supply_register(struct device *parent, struct power_supply *psy) 90int power_supply_register(struct device *parent, struct power_supply *psy)
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index f1e00ff54ce8..7e3ad4f3b343 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -251,20 +251,23 @@ void rtc_update_irq(struct rtc_device *rtc,
251} 251}
252EXPORT_SYMBOL_GPL(rtc_update_irq); 252EXPORT_SYMBOL_GPL(rtc_update_irq);
253 253
254static int __rtc_match(struct device *dev, void *data)
255{
256 char *name = (char *)data;
257
258 if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0)
259 return 1;
260 return 0;
261}
262
254struct rtc_device *rtc_class_open(char *name) 263struct rtc_device *rtc_class_open(char *name)
255{ 264{
256 struct device *dev; 265 struct device *dev;
257 struct rtc_device *rtc = NULL; 266 struct rtc_device *rtc = NULL;
258 267
259 down(&rtc_class->sem); 268 dev = class_find_device(rtc_class, name, __rtc_match);
260 list_for_each_entry(dev, &rtc_class->devices, node) { 269 if (dev)
261 if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0) { 270 rtc = to_rtc_device(dev);
262 dev = get_device(dev);
263 if (dev)
264 rtc = to_rtc_device(dev);
265 break;
266 }
267 }
268 271
269 if (rtc) { 272 if (rtc) {
270 if (!try_module_get(rtc->owner)) { 273 if (!try_module_get(rtc->owner)) {
@@ -272,7 +275,6 @@ struct rtc_device *rtc_class_open(char *name)
272 rtc = NULL; 275 rtc = NULL;
273 } 276 }
274 } 277 }
275 up(&rtc_class->sem);
276 278
277 return rtc; 279 return rtc;
278} 280}
diff --git a/drivers/s390/net/netiucv.c b/drivers/s390/net/netiucv.c
index c7ea9381db9f..d6e93f15440e 100644
--- a/drivers/s390/net/netiucv.c
+++ b/drivers/s390/net/netiucv.c
@@ -2089,6 +2089,11 @@ static struct attribute_group netiucv_drv_attr_group = {
2089 .attrs = netiucv_drv_attrs, 2089 .attrs = netiucv_drv_attrs,
2090}; 2090};
2091 2091
2092static struct attribute_group *netiucv_drv_attr_groups[] = {
2093 &netiucv_drv_attr_group,
2094 NULL,
2095};
2096
2092static void netiucv_banner(void) 2097static void netiucv_banner(void)
2093{ 2098{
2094 PRINT_INFO("NETIUCV driver initialized\n"); 2099 PRINT_INFO("NETIUCV driver initialized\n");
@@ -2113,7 +2118,6 @@ static void __exit netiucv_exit(void)
2113 netiucv_unregister_device(dev); 2118 netiucv_unregister_device(dev);
2114 } 2119 }
2115 2120
2116 sysfs_remove_group(&netiucv_driver.kobj, &netiucv_drv_attr_group);
2117 driver_unregister(&netiucv_driver); 2121 driver_unregister(&netiucv_driver);
2118 iucv_unregister(&netiucv_handler, 1); 2122 iucv_unregister(&netiucv_handler, 1);
2119 iucv_unregister_dbf_views(); 2123 iucv_unregister_dbf_views();
@@ -2133,6 +2137,7 @@ static int __init netiucv_init(void)
2133 if (rc) 2137 if (rc)
2134 goto out_dbf; 2138 goto out_dbf;
2135 IUCV_DBF_TEXT(trace, 3, __FUNCTION__); 2139 IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
2140 netiucv_driver.groups = netiucv_drv_attr_groups;
2136 rc = driver_register(&netiucv_driver); 2141 rc = driver_register(&netiucv_driver);
2137 if (rc) { 2142 if (rc) {
2138 PRINT_ERR("NETIUCV: failed to register driver.\n"); 2143 PRINT_ERR("NETIUCV: failed to register driver.\n");
@@ -2140,18 +2145,9 @@ static int __init netiucv_init(void)
2140 goto out_iucv; 2145 goto out_iucv;
2141 } 2146 }
2142 2147
2143 rc = sysfs_create_group(&netiucv_driver.kobj, &netiucv_drv_attr_group);
2144 if (rc) {
2145 PRINT_ERR("NETIUCV: failed to add driver attributes.\n");
2146 IUCV_DBF_TEXT_(setup, 2,
2147 "ret %d - netiucv_drv_attr_group\n", rc);
2148 goto out_driver;
2149 }
2150 netiucv_banner(); 2148 netiucv_banner();
2151 return rc; 2149 return rc;
2152 2150
2153out_driver:
2154 driver_unregister(&netiucv_driver);
2155out_iucv: 2151out_iucv:
2156 iucv_unregister(&netiucv_handler, 1); 2152 iucv_unregister(&netiucv_handler, 1);
2157out_dbf: 2153out_dbf:
diff --git a/drivers/s390/scsi/zfcp_ccw.c b/drivers/s390/scsi/zfcp_ccw.c
index e01cbf152a81..86c3f6539a7d 100644
--- a/drivers/s390/scsi/zfcp_ccw.c
+++ b/drivers/s390/scsi/zfcp_ccw.c
@@ -52,6 +52,9 @@ static struct ccw_driver zfcp_ccw_driver = {
52 .set_offline = zfcp_ccw_set_offline, 52 .set_offline = zfcp_ccw_set_offline,
53 .notify = zfcp_ccw_notify, 53 .notify = zfcp_ccw_notify,
54 .shutdown = zfcp_ccw_shutdown, 54 .shutdown = zfcp_ccw_shutdown,
55 .driver = {
56 .groups = zfcp_driver_attr_groups,
57 },
55}; 58};
56 59
57MODULE_DEVICE_TABLE(ccw, zfcp_ccw_device_id); 60MODULE_DEVICE_TABLE(ccw, zfcp_ccw_device_id);
@@ -251,16 +254,7 @@ zfcp_ccw_notify(struct ccw_device *ccw_device, int event)
251int __init 254int __init
252zfcp_ccw_register(void) 255zfcp_ccw_register(void)
253{ 256{
254 int retval; 257 return ccw_driver_register(&zfcp_ccw_driver);
255
256 retval = ccw_driver_register(&zfcp_ccw_driver);
257 if (retval)
258 goto out;
259 retval = zfcp_sysfs_driver_create_files(&zfcp_ccw_driver.driver);
260 if (retval)
261 ccw_driver_unregister(&zfcp_ccw_driver);
262 out:
263 return retval;
264} 258}
265 259
266/** 260/**
diff --git a/drivers/s390/scsi/zfcp_ext.h b/drivers/s390/scsi/zfcp_ext.h
index 8534cf09546c..06b1079b7f3d 100644
--- a/drivers/s390/scsi/zfcp_ext.h
+++ b/drivers/s390/scsi/zfcp_ext.h
@@ -27,8 +27,7 @@
27extern struct zfcp_data zfcp_data; 27extern struct zfcp_data zfcp_data;
28 28
29/******************************** SYSFS *************************************/ 29/******************************** SYSFS *************************************/
30extern int zfcp_sysfs_driver_create_files(struct device_driver *); 30extern struct attribute_group *zfcp_driver_attr_groups[];
31extern void zfcp_sysfs_driver_remove_files(struct device_driver *);
32extern int zfcp_sysfs_adapter_create_files(struct device *); 31extern int zfcp_sysfs_adapter_create_files(struct device *);
33extern void zfcp_sysfs_adapter_remove_files(struct device *); 32extern void zfcp_sysfs_adapter_remove_files(struct device *);
34extern int zfcp_sysfs_port_create_files(struct device *, u32); 33extern int zfcp_sysfs_port_create_files(struct device *, u32);
diff --git a/drivers/s390/scsi/zfcp_sysfs_driver.c b/drivers/s390/scsi/zfcp_sysfs_driver.c
index 005e62f8593b..651edd58906a 100644
--- a/drivers/s390/scsi/zfcp_sysfs_driver.c
+++ b/drivers/s390/scsi/zfcp_sysfs_driver.c
@@ -98,28 +98,9 @@ static struct attribute_group zfcp_driver_attr_group = {
98 .attrs = zfcp_driver_attrs, 98 .attrs = zfcp_driver_attrs,
99}; 99};
100 100
101/** 101struct attribute_group *zfcp_driver_attr_groups[] = {
102 * zfcp_sysfs_create_driver_files - create sysfs driver files 102 &zfcp_driver_attr_group,
103 * @dev: pointer to belonging device 103 NULL,
104 * 104};
105 * Create all sysfs attributes of the zfcp device driver
106 */
107int
108zfcp_sysfs_driver_create_files(struct device_driver *drv)
109{
110 return sysfs_create_group(&drv->kobj, &zfcp_driver_attr_group);
111}
112
113/**
114 * zfcp_sysfs_remove_driver_files - remove sysfs driver files
115 * @dev: pointer to belonging device
116 *
117 * Remove all sysfs attributes of the zfcp device driver
118 */
119void
120zfcp_sysfs_driver_remove_files(struct device_driver *drv)
121{
122 sysfs_remove_group(&drv->kobj, &zfcp_driver_attr_group);
123}
124 105
125#undef ZFCP_LOG_AREA 106#undef ZFCP_LOG_AREA
diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c
index 24271a871b8c..6325115e5b3d 100644
--- a/drivers/scsi/hosts.c
+++ b/drivers/scsi/hosts.c
@@ -429,6 +429,15 @@ void scsi_unregister(struct Scsi_Host *shost)
429} 429}
430EXPORT_SYMBOL(scsi_unregister); 430EXPORT_SYMBOL(scsi_unregister);
431 431
432static int __scsi_host_match(struct class_device *cdev, void *data)
433{
434 struct Scsi_Host *p;
435 unsigned short *hostnum = (unsigned short *)data;
436
437 p = class_to_shost(cdev);
438 return p->host_no == *hostnum;
439}
440
432/** 441/**
433 * scsi_host_lookup - get a reference to a Scsi_Host by host no 442 * scsi_host_lookup - get a reference to a Scsi_Host by host no
434 * 443 *
@@ -439,19 +448,12 @@ EXPORT_SYMBOL(scsi_unregister);
439 **/ 448 **/
440struct Scsi_Host *scsi_host_lookup(unsigned short hostnum) 449struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
441{ 450{
442 struct class *class = &shost_class;
443 struct class_device *cdev; 451 struct class_device *cdev;
444 struct Scsi_Host *shost = ERR_PTR(-ENXIO), *p; 452 struct Scsi_Host *shost = ERR_PTR(-ENXIO);
445 453
446 down(&class->sem); 454 cdev = class_find_child(&shost_class, &hostnum, __scsi_host_match);
447 list_for_each_entry(cdev, &class->children, node) { 455 if (cdev)
448 p = class_to_shost(cdev); 456 shost = scsi_host_get(class_to_shost(cdev));
449 if (p->host_no == hostnum) {
450 shost = scsi_host_get(p);
451 break;
452 }
453 }
454 up(&class->sem);
455 457
456 return shost; 458 return shost;
457} 459}
diff --git a/drivers/scsi/libsas/sas_scsi_host.c b/drivers/scsi/libsas/sas_scsi_host.c
index 7663841eb4cf..a3fdc57e2673 100644
--- a/drivers/scsi/libsas/sas_scsi_host.c
+++ b/drivers/scsi/libsas/sas_scsi_host.c
@@ -464,7 +464,7 @@ int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
464 res = sas_phy_reset(phy, 1); 464 res = sas_phy_reset(phy, 1);
465 if (res) 465 if (res)
466 SAS_DPRINTK("Bus reset of %s failed 0x%x\n", 466 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
467 phy->dev.kobj.k_name, 467 kobject_name(&phy->dev.kobj),
468 res); 468 res);
469 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 469 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
470 return SUCCESS; 470 return SUCCESS;
diff --git a/drivers/serial/icom.c b/drivers/serial/icom.c
index 9d3105b64a7a..9c2df5c857cf 100644
--- a/drivers/serial/icom.c
+++ b/drivers/serial/icom.c
@@ -48,7 +48,7 @@
48#include <linux/vmalloc.h> 48#include <linux/vmalloc.h>
49#include <linux/smp.h> 49#include <linux/smp.h>
50#include <linux/spinlock.h> 50#include <linux/spinlock.h>
51#include <linux/kobject.h> 51#include <linux/kref.h>
52#include <linux/firmware.h> 52#include <linux/firmware.h>
53#include <linux/bitops.h> 53#include <linux/bitops.h>
54 54
@@ -65,7 +65,7 @@
65#define ICOM_VERSION_STR "1.3.1" 65#define ICOM_VERSION_STR "1.3.1"
66#define NR_PORTS 128 66#define NR_PORTS 128
67#define ICOM_PORT ((struct icom_port *)port) 67#define ICOM_PORT ((struct icom_port *)port)
68#define to_icom_adapter(d) container_of(d, struct icom_adapter, kobj) 68#define to_icom_adapter(d) container_of(d, struct icom_adapter, kref)
69 69
70static const struct pci_device_id icom_pci_table[] = { 70static const struct pci_device_id icom_pci_table[] = {
71 { 71 {
@@ -141,6 +141,7 @@ static inline void trace(struct icom_port *, char *, unsigned long) {};
141#else 141#else
142static inline void trace(struct icom_port *icom_port, char *trace_pt, unsigned long trace_data) {}; 142static inline void trace(struct icom_port *icom_port, char *trace_pt, unsigned long trace_data) {};
143#endif 143#endif
144static void icom_kref_release(struct kref *kref);
144 145
145static void free_port_memory(struct icom_port *icom_port) 146static void free_port_memory(struct icom_port *icom_port)
146{ 147{
@@ -1063,11 +1064,11 @@ static int icom_open(struct uart_port *port)
1063{ 1064{
1064 int retval; 1065 int retval;
1065 1066
1066 kobject_get(&ICOM_PORT->adapter->kobj); 1067 kref_get(&ICOM_PORT->adapter->kref);
1067 retval = startup(ICOM_PORT); 1068 retval = startup(ICOM_PORT);
1068 1069
1069 if (retval) { 1070 if (retval) {
1070 kobject_put(&ICOM_PORT->adapter->kobj); 1071 kref_put(&ICOM_PORT->adapter->kref, icom_kref_release);
1071 trace(ICOM_PORT, "STARTUP_ERROR", 0); 1072 trace(ICOM_PORT, "STARTUP_ERROR", 0);
1072 return retval; 1073 return retval;
1073 } 1074 }
@@ -1088,7 +1089,7 @@ static void icom_close(struct uart_port *port)
1088 1089
1089 shutdown(ICOM_PORT); 1090 shutdown(ICOM_PORT);
1090 1091
1091 kobject_put(&ICOM_PORT->adapter->kobj); 1092 kref_put(&ICOM_PORT->adapter->kref, icom_kref_release);
1092} 1093}
1093 1094
1094static void icom_set_termios(struct uart_port *port, 1095static void icom_set_termios(struct uart_port *port,
@@ -1485,18 +1486,14 @@ static void icom_remove_adapter(struct icom_adapter *icom_adapter)
1485 pci_release_regions(icom_adapter->pci_dev); 1486 pci_release_regions(icom_adapter->pci_dev);
1486} 1487}
1487 1488
1488static void icom_kobj_release(struct kobject *kobj) 1489static void icom_kref_release(struct kref *kref)
1489{ 1490{
1490 struct icom_adapter *icom_adapter; 1491 struct icom_adapter *icom_adapter;
1491 1492
1492 icom_adapter = to_icom_adapter(kobj); 1493 icom_adapter = to_icom_adapter(kref);
1493 icom_remove_adapter(icom_adapter); 1494 icom_remove_adapter(icom_adapter);
1494} 1495}
1495 1496
1496static struct kobj_type icom_kobj_type = {
1497 .release = icom_kobj_release,
1498};
1499
1500static int __devinit icom_probe(struct pci_dev *dev, 1497static int __devinit icom_probe(struct pci_dev *dev,
1501 const struct pci_device_id *ent) 1498 const struct pci_device_id *ent)
1502{ 1499{
@@ -1592,8 +1589,7 @@ static int __devinit icom_probe(struct pci_dev *dev,
1592 } 1589 }
1593 } 1590 }
1594 1591
1595 kobject_init(&icom_adapter->kobj); 1592 kref_init(&icom_adapter->kref);
1596 icom_adapter->kobj.ktype = &icom_kobj_type;
1597 return 0; 1593 return 0;
1598 1594
1599probe_exit2: 1595probe_exit2:
@@ -1619,7 +1615,7 @@ static void __devexit icom_remove(struct pci_dev *dev)
1619 icom_adapter = list_entry(tmp, struct icom_adapter, 1615 icom_adapter = list_entry(tmp, struct icom_adapter,
1620 icom_adapter_entry); 1616 icom_adapter_entry);
1621 if (icom_adapter->pci_dev == dev) { 1617 if (icom_adapter->pci_dev == dev) {
1622 kobject_put(&icom_adapter->kobj); 1618 kref_put(&icom_adapter->kref, icom_kref_release);
1623 return; 1619 return;
1624 } 1620 }
1625 } 1621 }
diff --git a/drivers/serial/icom.h b/drivers/serial/icom.h
index e8578d8cd35e..027455496745 100644
--- a/drivers/serial/icom.h
+++ b/drivers/serial/icom.h
@@ -270,7 +270,7 @@ struct icom_adapter {
270#define V2_ONE_PORT_RVX_ONE_PORT_IMBED_MDM 0x0251 270#define V2_ONE_PORT_RVX_ONE_PORT_IMBED_MDM 0x0251
271 int numb_ports; 271 int numb_ports;
272 struct list_head icom_adapter_entry; 272 struct list_head icom_adapter_entry;
273 struct kobject kobj; 273 struct kref kref;
274}; 274};
275 275
276/* prototype */ 276/* prototype */
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 93e9de46977a..682a6a48fec3 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -485,6 +485,15 @@ void spi_unregister_master(struct spi_master *master)
485} 485}
486EXPORT_SYMBOL_GPL(spi_unregister_master); 486EXPORT_SYMBOL_GPL(spi_unregister_master);
487 487
488static int __spi_master_match(struct device *dev, void *data)
489{
490 struct spi_master *m;
491 u16 *bus_num = data;
492
493 m = container_of(dev, struct spi_master, dev);
494 return m->bus_num == *bus_num;
495}
496
488/** 497/**
489 * spi_busnum_to_master - look up master associated with bus_num 498 * spi_busnum_to_master - look up master associated with bus_num
490 * @bus_num: the master's bus number 499 * @bus_num: the master's bus number
@@ -499,17 +508,12 @@ struct spi_master *spi_busnum_to_master(u16 bus_num)
499{ 508{
500 struct device *dev; 509 struct device *dev;
501 struct spi_master *master = NULL; 510 struct spi_master *master = NULL;
502 struct spi_master *m; 511
503 512 dev = class_find_device(&spi_master_class, &bus_num,
504 down(&spi_master_class.sem); 513 __spi_master_match);
505 list_for_each_entry(dev, &spi_master_class.children, node) { 514 if (dev)
506 m = container_of(dev, struct spi_master, dev); 515 master = container_of(dev, struct spi_master, dev);
507 if (m->bus_num == bus_num) { 516 /* reference got in class_find_device */
508 master = spi_master_get(m);
509 break;
510 }
511 }
512 up(&spi_master_class.sem);
513 return master; 517 return master;
514} 518}
515EXPORT_SYMBOL_GPL(spi_busnum_to_master); 519EXPORT_SYMBOL_GPL(spi_busnum_to_master);
diff --git a/drivers/uio/uio.c b/drivers/uio/uio.c
index 865f32b63b5c..cc246faa3590 100644
--- a/drivers/uio/uio.c
+++ b/drivers/uio/uio.c
@@ -34,12 +34,12 @@ struct uio_device {
34 wait_queue_head_t wait; 34 wait_queue_head_t wait;
35 int vma_count; 35 int vma_count;
36 struct uio_info *info; 36 struct uio_info *info;
37 struct kset map_attr_kset; 37 struct kobject *map_dir;
38}; 38};
39 39
40static int uio_major; 40static int uio_major;
41static DEFINE_IDR(uio_idr); 41static DEFINE_IDR(uio_idr);
42static struct file_operations uio_fops; 42static const struct file_operations uio_fops;
43 43
44/* UIO class infrastructure */ 44/* UIO class infrastructure */
45static struct uio_class { 45static struct uio_class {
@@ -51,47 +51,48 @@ static struct uio_class {
51 * attributes 51 * attributes
52 */ 52 */
53 53
54static struct attribute attr_addr = { 54struct uio_map {
55 .name = "addr", 55 struct kobject kobj;
56 .mode = S_IRUGO, 56 struct uio_mem *mem;
57}; 57};
58#define to_map(map) container_of(map, struct uio_map, kobj)
58 59
59static struct attribute attr_size = {
60 .name = "size",
61 .mode = S_IRUGO,
62};
63 60
64static struct attribute* map_attrs[] = { 61static ssize_t map_attr_show(struct kobject *kobj, struct kobj_attribute *attr,
65 &attr_addr, &attr_size, NULL
66};
67
68static ssize_t map_attr_show(struct kobject *kobj, struct attribute *attr,
69 char *buf) 62 char *buf)
70{ 63{
71 struct uio_mem *mem = container_of(kobj, struct uio_mem, kobj); 64 struct uio_map *map = to_map(kobj);
65 struct uio_mem *mem = map->mem;
72 66
73 if (strncmp(attr->name,"addr",4) == 0) 67 if (strncmp(attr->attr.name, "addr", 4) == 0)
74 return sprintf(buf, "0x%lx\n", mem->addr); 68 return sprintf(buf, "0x%lx\n", mem->addr);
75 69
76 if (strncmp(attr->name,"size",4) == 0) 70 if (strncmp(attr->attr.name, "size", 4) == 0)
77 return sprintf(buf, "0x%lx\n", mem->size); 71 return sprintf(buf, "0x%lx\n", mem->size);
78 72
79 return -ENODEV; 73 return -ENODEV;
80} 74}
81 75
82static void map_attr_release(struct kobject *kobj) 76static struct kobj_attribute attr_attribute =
83{ 77 __ATTR(addr, S_IRUGO, map_attr_show, NULL);
84 /* TODO ??? */ 78static struct kobj_attribute size_attribute =
85} 79 __ATTR(size, S_IRUGO, map_attr_show, NULL);
86 80
87static struct sysfs_ops map_attr_ops = { 81static struct attribute *attrs[] = {
88 .show = map_attr_show, 82 &attr_attribute.attr,
83 &size_attribute.attr,
84 NULL, /* need to NULL terminate the list of attributes */
89}; 85};
90 86
87static void map_release(struct kobject *kobj)
88{
89 struct uio_map *map = to_map(kobj);
90 kfree(map);
91}
92
91static struct kobj_type map_attr_type = { 93static struct kobj_type map_attr_type = {
92 .release = map_attr_release, 94 .release = map_release,
93 .sysfs_ops = &map_attr_ops, 95 .default_attrs = attrs,
94 .default_attrs = map_attrs,
95}; 96};
96 97
97static ssize_t show_name(struct device *dev, 98static ssize_t show_name(struct device *dev,
@@ -148,6 +149,7 @@ static int uio_dev_add_attributes(struct uio_device *idev)
148 int mi; 149 int mi;
149 int map_found = 0; 150 int map_found = 0;
150 struct uio_mem *mem; 151 struct uio_mem *mem;
152 struct uio_map *map;
151 153
152 ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp); 154 ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
153 if (ret) 155 if (ret)
@@ -159,31 +161,34 @@ static int uio_dev_add_attributes(struct uio_device *idev)
159 break; 161 break;
160 if (!map_found) { 162 if (!map_found) {
161 map_found = 1; 163 map_found = 1;
162 kobject_set_name(&idev->map_attr_kset.kobj,"maps"); 164 idev->map_dir = kobject_create_and_add("maps",
163 idev->map_attr_kset.ktype = &map_attr_type; 165 &idev->dev->kobj);
164 idev->map_attr_kset.kobj.parent = &idev->dev->kobj; 166 if (!idev->map_dir)
165 ret = kset_register(&idev->map_attr_kset); 167 goto err;
166 if (ret)
167 goto err_remove_group;
168 } 168 }
169 kobject_init(&mem->kobj); 169 map = kzalloc(sizeof(*map), GFP_KERNEL);
170 kobject_set_name(&mem->kobj,"map%d",mi); 170 if (!map)
171 mem->kobj.parent = &idev->map_attr_kset.kobj; 171 goto err;
172 mem->kobj.kset = &idev->map_attr_kset; 172 kobject_init(&map->kobj, &map_attr_type);
173 ret = kobject_add(&mem->kobj); 173 map->mem = mem;
174 mem->map = map;
175 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
176 if (ret)
177 goto err;
178 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
174 if (ret) 179 if (ret)
175 goto err_remove_maps; 180 goto err;
176 } 181 }
177 182
178 return 0; 183 return 0;
179 184
180err_remove_maps: 185err:
181 for (mi--; mi>=0; mi--) { 186 for (mi--; mi>=0; mi--) {
182 mem = &idev->info->mem[mi]; 187 mem = &idev->info->mem[mi];
183 kobject_unregister(&mem->kobj); 188 map = mem->map;
189 kobject_put(&map->kobj);
184 } 190 }
185 kset_unregister(&idev->map_attr_kset); /* Needed ? */ 191 kobject_put(idev->map_dir);
186err_remove_group:
187 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp); 192 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
188err_group: 193err_group:
189 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret); 194 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
@@ -198,9 +203,9 @@ static void uio_dev_del_attributes(struct uio_device *idev)
198 mem = &idev->info->mem[mi]; 203 mem = &idev->info->mem[mi];
199 if (mem->size == 0) 204 if (mem->size == 0)
200 break; 205 break;
201 kobject_unregister(&mem->kobj); 206 kobject_put(&mem->map->kobj);
202 } 207 }
203 kset_unregister(&idev->map_attr_kset); 208 kobject_put(idev->map_dir);
204 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp); 209 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
205} 210}
206 211
@@ -503,7 +508,7 @@ static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
503 } 508 }
504} 509}
505 510
506static struct file_operations uio_fops = { 511static const struct file_operations uio_fops = {
507 .owner = THIS_MODULE, 512 .owner = THIS_MODULE,
508 .open = uio_open, 513 .open = uio_open,
509 .release = uio_release, 514 .release = uio_release,
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index c51f8e9312e0..7c3aaa9c5402 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -91,8 +91,8 @@ static int usb_create_newid_file(struct usb_driver *usb_drv)
91 goto exit; 91 goto exit;
92 92
93 if (usb_drv->probe != NULL) 93 if (usb_drv->probe != NULL)
94 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj, 94 error = driver_create_file(&usb_drv->drvwrap.driver,
95 &driver_attr_new_id.attr); 95 &driver_attr_new_id);
96exit: 96exit:
97 return error; 97 return error;
98} 98}
@@ -103,8 +103,8 @@ static void usb_remove_newid_file(struct usb_driver *usb_drv)
103 return; 103 return;
104 104
105 if (usb_drv->probe != NULL) 105 if (usb_drv->probe != NULL)
106 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj, 106 driver_remove_file(&usb_drv->drvwrap.driver,
107 &driver_attr_new_id.attr); 107 &driver_attr_new_id);
108} 108}
109 109
110static void usb_free_dynids(struct usb_driver *usb_drv) 110static void usb_free_dynids(struct usb_driver *usb_drv)
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 993f78c55221..e48a630ae266 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -738,9 +738,9 @@ EXPORT_SYMBOL(bd_release);
738static struct kobject *bdev_get_kobj(struct block_device *bdev) 738static struct kobject *bdev_get_kobj(struct block_device *bdev)
739{ 739{
740 if (bdev->bd_contains != bdev) 740 if (bdev->bd_contains != bdev)
741 return kobject_get(&bdev->bd_part->kobj); 741 return kobject_get(&bdev->bd_part->dev.kobj);
742 else 742 else
743 return kobject_get(&bdev->bd_disk->kobj); 743 return kobject_get(&bdev->bd_disk->dev.kobj);
744} 744}
745 745
746static struct kobject *bdev_get_holder(struct block_device *bdev) 746static struct kobject *bdev_get_holder(struct block_device *bdev)
@@ -1176,7 +1176,7 @@ static int do_open(struct block_device *bdev, struct file *file, int for_part)
1176 ret = -ENXIO; 1176 ret = -ENXIO;
1177 goto out_first; 1177 goto out_first;
1178 } 1178 }
1179 kobject_get(&p->kobj); 1179 kobject_get(&p->dev.kobj);
1180 bdev->bd_part = p; 1180 bdev->bd_part = p;
1181 bd_set_size(bdev, (loff_t) p->nr_sects << 9); 1181 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
1182 } 1182 }
@@ -1299,7 +1299,7 @@ static int __blkdev_put(struct block_device *bdev, int for_part)
1299 module_put(owner); 1299 module_put(owner);
1300 1300
1301 if (bdev->bd_contains != bdev) { 1301 if (bdev->bd_contains != bdev) {
1302 kobject_put(&bdev->bd_part->kobj); 1302 kobject_put(&bdev->bd_part->dev.kobj);
1303 bdev->bd_part = NULL; 1303 bdev->bd_part = NULL;
1304 } 1304 }
1305 bdev->bd_disk = NULL; 1305 bdev->bd_disk = NULL;
diff --git a/fs/char_dev.c b/fs/char_dev.c
index c3bfa76765c4..2c7a8b5b4598 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -510,9 +510,8 @@ struct cdev *cdev_alloc(void)
510{ 510{
511 struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL); 511 struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
512 if (p) { 512 if (p) {
513 p->kobj.ktype = &ktype_cdev_dynamic;
514 INIT_LIST_HEAD(&p->list); 513 INIT_LIST_HEAD(&p->list);
515 kobject_init(&p->kobj); 514 kobject_init(&p->kobj, &ktype_cdev_dynamic);
516 } 515 }
517 return p; 516 return p;
518} 517}
@@ -529,8 +528,7 @@ void cdev_init(struct cdev *cdev, const struct file_operations *fops)
529{ 528{
530 memset(cdev, 0, sizeof *cdev); 529 memset(cdev, 0, sizeof *cdev);
531 INIT_LIST_HEAD(&cdev->list); 530 INIT_LIST_HEAD(&cdev->list);
532 cdev->kobj.ktype = &ktype_cdev_default; 531 kobject_init(&cdev->kobj, &ktype_cdev_default);
533 kobject_init(&cdev->kobj);
534 cdev->ops = fops; 532 cdev->ops = fops;
535} 533}
536 534
diff --git a/fs/coda/psdev.c b/fs/coda/psdev.c
index dcc6aead70f5..e3eb3556622b 100644
--- a/fs/coda/psdev.c
+++ b/fs/coda/psdev.c
@@ -362,8 +362,8 @@ static int init_coda_psdev(void)
362 goto out_chrdev; 362 goto out_chrdev;
363 } 363 }
364 for (i = 0; i < MAX_CODADEVS; i++) 364 for (i = 0; i < MAX_CODADEVS; i++)
365 class_device_create(coda_psdev_class, NULL, 365 device_create(coda_psdev_class, NULL,
366 MKDEV(CODA_PSDEV_MAJOR,i), NULL, "cfs%d", i); 366 MKDEV(CODA_PSDEV_MAJOR,i), "cfs%d", i);
367 coda_sysctl_init(); 367 coda_sysctl_init();
368 goto out; 368 goto out;
369 369
@@ -405,7 +405,7 @@ static int __init init_coda(void)
405 return 0; 405 return 0;
406out: 406out:
407 for (i = 0; i < MAX_CODADEVS; i++) 407 for (i = 0; i < MAX_CODADEVS; i++)
408 class_device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i)); 408 device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
409 class_destroy(coda_psdev_class); 409 class_destroy(coda_psdev_class);
410 unregister_chrdev(CODA_PSDEV_MAJOR, "coda"); 410 unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
411 coda_sysctl_clean(); 411 coda_sysctl_clean();
@@ -424,7 +424,7 @@ static void __exit exit_coda(void)
424 printk("coda: failed to unregister filesystem\n"); 424 printk("coda: failed to unregister filesystem\n");
425 } 425 }
426 for (i = 0; i < MAX_CODADEVS; i++) 426 for (i = 0; i < MAX_CODADEVS; i++)
427 class_device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i)); 427 device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
428 class_destroy(coda_psdev_class); 428 class_destroy(coda_psdev_class);
429 unregister_chrdev(CODA_PSDEV_MAJOR, "coda"); 429 unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
430 coda_sysctl_clean(); 430 coda_sysctl_clean();
diff --git a/fs/configfs/mount.c b/fs/configfs/mount.c
index 3bf0278ea843..de3b31d0a37d 100644
--- a/fs/configfs/mount.c
+++ b/fs/configfs/mount.c
@@ -128,7 +128,7 @@ void configfs_release_fs(void)
128} 128}
129 129
130 130
131static decl_subsys(config, NULL, NULL); 131static struct kobject *config_kobj;
132 132
133static int __init configfs_init(void) 133static int __init configfs_init(void)
134{ 134{
@@ -140,9 +140,8 @@ static int __init configfs_init(void)
140 if (!configfs_dir_cachep) 140 if (!configfs_dir_cachep)
141 goto out; 141 goto out;
142 142
143 kobj_set_kset_s(&config_subsys, kernel_subsys); 143 config_kobj = kobject_create_and_add("config", kernel_kobj);
144 err = subsystem_register(&config_subsys); 144 if (!config_kobj) {
145 if (err) {
146 kmem_cache_destroy(configfs_dir_cachep); 145 kmem_cache_destroy(configfs_dir_cachep);
147 configfs_dir_cachep = NULL; 146 configfs_dir_cachep = NULL;
148 goto out; 147 goto out;
@@ -151,7 +150,7 @@ static int __init configfs_init(void)
151 err = register_filesystem(&configfs_fs_type); 150 err = register_filesystem(&configfs_fs_type);
152 if (err) { 151 if (err) {
153 printk(KERN_ERR "configfs: Unable to register filesystem!\n"); 152 printk(KERN_ERR "configfs: Unable to register filesystem!\n");
154 subsystem_unregister(&config_subsys); 153 kobject_put(config_kobj);
155 kmem_cache_destroy(configfs_dir_cachep); 154 kmem_cache_destroy(configfs_dir_cachep);
156 configfs_dir_cachep = NULL; 155 configfs_dir_cachep = NULL;
157 goto out; 156 goto out;
@@ -160,7 +159,7 @@ static int __init configfs_init(void)
160 err = configfs_inode_init(); 159 err = configfs_inode_init();
161 if (err) { 160 if (err) {
162 unregister_filesystem(&configfs_fs_type); 161 unregister_filesystem(&configfs_fs_type);
163 subsystem_unregister(&config_subsys); 162 kobject_put(config_kobj);
164 kmem_cache_destroy(configfs_dir_cachep); 163 kmem_cache_destroy(configfs_dir_cachep);
165 configfs_dir_cachep = NULL; 164 configfs_dir_cachep = NULL;
166 } 165 }
@@ -171,7 +170,7 @@ out:
171static void __exit configfs_exit(void) 170static void __exit configfs_exit(void)
172{ 171{
173 unregister_filesystem(&configfs_fs_type); 172 unregister_filesystem(&configfs_fs_type);
174 subsystem_unregister(&config_subsys); 173 kobject_put(config_kobj);
175 kmem_cache_destroy(configfs_dir_cachep); 174 kmem_cache_destroy(configfs_dir_cachep);
176 configfs_dir_cachep = NULL; 175 configfs_dir_cachep = NULL;
177 configfs_inode_exit(); 176 configfs_inode_exit();
diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
index 6a713b33992f..d26e2826ba5b 100644
--- a/fs/debugfs/inode.c
+++ b/fs/debugfs/inode.c
@@ -426,20 +426,19 @@ exit:
426} 426}
427EXPORT_SYMBOL_GPL(debugfs_rename); 427EXPORT_SYMBOL_GPL(debugfs_rename);
428 428
429static decl_subsys(debug, NULL, NULL); 429static struct kobject *debug_kobj;
430 430
431static int __init debugfs_init(void) 431static int __init debugfs_init(void)
432{ 432{
433 int retval; 433 int retval;
434 434
435 kobj_set_kset_s(&debug_subsys, kernel_subsys); 435 debug_kobj = kobject_create_and_add("debug", kernel_kobj);
436 retval = subsystem_register(&debug_subsys); 436 if (!debug_kobj)
437 if (retval) 437 return -EINVAL;
438 return retval;
439 438
440 retval = register_filesystem(&debug_fs_type); 439 retval = register_filesystem(&debug_fs_type);
441 if (retval) 440 if (retval)
442 subsystem_unregister(&debug_subsys); 441 kobject_put(debug_kobj);
443 return retval; 442 return retval;
444} 443}
445 444
@@ -447,7 +446,7 @@ static void __exit debugfs_exit(void)
447{ 446{
448 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 447 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
449 unregister_filesystem(&debug_fs_type); 448 unregister_filesystem(&debug_fs_type);
450 subsystem_unregister(&debug_subsys); 449 kobject_put(debug_kobj);
451} 450}
452 451
453core_initcall(debugfs_init); 452core_initcall(debugfs_init);
diff --git a/fs/dlm/lockspace.c b/fs/dlm/lockspace.c
index 6353a8384520..5c108c49cb8c 100644
--- a/fs/dlm/lockspace.c
+++ b/fs/dlm/lockspace.c
@@ -166,26 +166,7 @@ static struct kobj_type dlm_ktype = {
166 .release = lockspace_kobj_release, 166 .release = lockspace_kobj_release,
167}; 167};
168 168
169static struct kset dlm_kset = { 169static struct kset *dlm_kset;
170 .ktype = &dlm_ktype,
171};
172
173static int kobject_setup(struct dlm_ls *ls)
174{
175 char lsname[DLM_LOCKSPACE_LEN];
176 int error;
177
178 memset(lsname, 0, DLM_LOCKSPACE_LEN);
179 snprintf(lsname, DLM_LOCKSPACE_LEN, "%s", ls->ls_name);
180
181 error = kobject_set_name(&ls->ls_kobj, "%s", lsname);
182 if (error)
183 return error;
184
185 ls->ls_kobj.kset = &dlm_kset;
186 ls->ls_kobj.ktype = &dlm_ktype;
187 return 0;
188}
189 170
190static int do_uevent(struct dlm_ls *ls, int in) 171static int do_uevent(struct dlm_ls *ls, int in)
191{ 172{
@@ -220,24 +201,22 @@ static int do_uevent(struct dlm_ls *ls, int in)
220 201
221int dlm_lockspace_init(void) 202int dlm_lockspace_init(void)
222{ 203{
223 int error;
224
225 ls_count = 0; 204 ls_count = 0;
226 mutex_init(&ls_lock); 205 mutex_init(&ls_lock);
227 INIT_LIST_HEAD(&lslist); 206 INIT_LIST_HEAD(&lslist);
228 spin_lock_init(&lslist_lock); 207 spin_lock_init(&lslist_lock);
229 208
230 kobject_set_name(&dlm_kset.kobj, "dlm"); 209 dlm_kset = kset_create_and_add("dlm", NULL, kernel_kobj);
231 kobj_set_kset_s(&dlm_kset, kernel_subsys); 210 if (!dlm_kset) {
232 error = kset_register(&dlm_kset); 211 printk(KERN_WARNING "%s: can not create kset\n", __FUNCTION__);
233 if (error) 212 return -ENOMEM;
234 printk("dlm_lockspace_init: cannot register kset %d\n", error); 213 }
235 return error; 214 return 0;
236} 215}
237 216
238void dlm_lockspace_exit(void) 217void dlm_lockspace_exit(void)
239{ 218{
240 kset_unregister(&dlm_kset); 219 kset_unregister(dlm_kset);
241} 220}
242 221
243static int dlm_scand(void *data) 222static int dlm_scand(void *data)
@@ -549,13 +528,12 @@ static int new_lockspace(char *name, int namelen, void **lockspace,
549 goto out_delist; 528 goto out_delist;
550 } 529 }
551 530
552 error = kobject_setup(ls); 531 ls->ls_kobj.kset = dlm_kset;
553 if (error) 532 error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL,
554 goto out_stop; 533 "%s", ls->ls_name);
555
556 error = kobject_register(&ls->ls_kobj);
557 if (error) 534 if (error)
558 goto out_stop; 535 goto out_stop;
536 kobject_uevent(&ls->ls_kobj, KOBJ_ADD);
559 537
560 /* let kobject handle freeing of ls if there's an error */ 538 /* let kobject handle freeing of ls if there's an error */
561 do_unreg = 1; 539 do_unreg = 1;
@@ -601,7 +579,7 @@ static int new_lockspace(char *name, int namelen, void **lockspace,
601 kfree(ls->ls_rsbtbl); 579 kfree(ls->ls_rsbtbl);
602 out_lsfree: 580 out_lsfree:
603 if (do_unreg) 581 if (do_unreg)
604 kobject_unregister(&ls->ls_kobj); 582 kobject_put(&ls->ls_kobj);
605 else 583 else
606 kfree(ls); 584 kfree(ls);
607 out: 585 out:
@@ -750,7 +728,7 @@ static int release_lockspace(struct dlm_ls *ls, int force)
750 dlm_clear_members(ls); 728 dlm_clear_members(ls);
751 dlm_clear_members_gone(ls); 729 dlm_clear_members_gone(ls);
752 kfree(ls->ls_node_array); 730 kfree(ls->ls_node_array);
753 kobject_unregister(&ls->ls_kobj); 731 kobject_put(&ls->ls_kobj);
754 /* The ls structure will be freed when the kobject is done with */ 732 /* The ls structure will be freed when the kobject is done with */
755 733
756 mutex_lock(&ls_lock); 734 mutex_lock(&ls_lock);
diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
index e5580bcb923a..0249aa4ae181 100644
--- a/fs/ecryptfs/main.c
+++ b/fs/ecryptfs/main.c
@@ -734,127 +734,40 @@ static int ecryptfs_init_kmem_caches(void)
734 return 0; 734 return 0;
735} 735}
736 736
737struct ecryptfs_obj { 737static struct kobject *ecryptfs_kobj;
738 char *name;
739 struct list_head slot_list;
740 struct kobject kobj;
741};
742
743struct ecryptfs_attribute {
744 struct attribute attr;
745 ssize_t(*show) (struct ecryptfs_obj *, char *);
746 ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
747};
748 738
749static ssize_t 739static ssize_t version_show(struct kobject *kobj,
750ecryptfs_attr_store(struct kobject *kobj, 740 struct kobj_attribute *attr, char *buff)
751 struct attribute *attr, const char *buf, size_t len)
752{ 741{
753 struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj, 742 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
754 kobj);
755 struct ecryptfs_attribute *attribute =
756 container_of(attr, struct ecryptfs_attribute, attr);
757
758 return (attribute->store ? attribute->store(obj, buf, len) : 0);
759} 743}
760 744
761static ssize_t 745static struct kobj_attribute version_attr = __ATTR_RO(version);
762ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
763{
764 struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
765 kobj);
766 struct ecryptfs_attribute *attribute =
767 container_of(attr, struct ecryptfs_attribute, attr);
768
769 return (attribute->show ? attribute->show(obj, buf) : 0);
770}
771 746
772static struct sysfs_ops ecryptfs_sysfs_ops = { 747static struct attribute *attributes[] = {
773 .show = ecryptfs_attr_show, 748 &version_attr.attr,
774 .store = ecryptfs_attr_store 749 NULL,
775}; 750};
776 751
777static struct kobj_type ecryptfs_ktype = { 752static struct attribute_group attr_group = {
778 .sysfs_ops = &ecryptfs_sysfs_ops 753 .attrs = attributes,
779}; 754};
780 755
781static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
782
783static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
784{
785 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
786}
787
788static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
789
790static struct ecryptfs_version_str_map_elem {
791 u32 flag;
792 char *str;
793} ecryptfs_version_str_map[] = {
794 {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
795 {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
796 {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
797 {ECRYPTFS_VERSIONING_POLICY, "policy"},
798 {ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"},
799 {ECRYPTFS_VERSIONING_MULTKEY, "multiple keys per file"}
800};
801
802static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
803{
804 int i;
805 int remaining = PAGE_SIZE;
806 int total_written = 0;
807
808 buff[0] = '\0';
809 for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
810 int entry_size;
811
812 if (!(ECRYPTFS_VERSIONING_MASK
813 & ecryptfs_version_str_map[i].flag))
814 continue;
815 entry_size = strlen(ecryptfs_version_str_map[i].str);
816 if ((entry_size + 2) > remaining)
817 goto out;
818 memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
819 buff[entry_size++] = '\n';
820 buff[entry_size] = '\0';
821 buff += entry_size;
822 total_written += entry_size;
823 remaining -= entry_size;
824 }
825out:
826 return total_written;
827}
828
829static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
830
831static int do_sysfs_registration(void) 756static int do_sysfs_registration(void)
832{ 757{
833 int rc; 758 int rc;
834 759
835 rc = subsystem_register(&ecryptfs_subsys); 760 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
836 if (rc) { 761 if (!ecryptfs_kobj) {
837 printk(KERN_ERR 762 printk(KERN_ERR "Unable to create ecryptfs kset\n");
838 "Unable to register ecryptfs sysfs subsystem\n"); 763 rc = -ENOMEM;
839 goto out;
840 }
841 rc = sysfs_create_file(&ecryptfs_subsys.kobj,
842 &sysfs_attr_version.attr);
843 if (rc) {
844 printk(KERN_ERR
845 "Unable to create ecryptfs version attribute\n");
846 subsystem_unregister(&ecryptfs_subsys);
847 goto out; 764 goto out;
848 } 765 }
849 rc = sysfs_create_file(&ecryptfs_subsys.kobj, 766 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
850 &sysfs_attr_version_str.attr);
851 if (rc) { 767 if (rc) {
852 printk(KERN_ERR 768 printk(KERN_ERR
853 "Unable to create ecryptfs version_str attribute\n"); 769 "Unable to create ecryptfs version attributes\n");
854 sysfs_remove_file(&ecryptfs_subsys.kobj, 770 kobject_put(ecryptfs_kobj);
855 &sysfs_attr_version.attr);
856 subsystem_unregister(&ecryptfs_subsys);
857 goto out;
858 } 771 }
859out: 772out:
860 return rc; 773 return rc;
@@ -862,11 +775,8 @@ out:
862 775
863static void do_sysfs_unregistration(void) 776static void do_sysfs_unregistration(void)
864{ 777{
865 sysfs_remove_file(&ecryptfs_subsys.kobj, 778 sysfs_remove_group(ecryptfs_kobj, &attr_group);
866 &sysfs_attr_version.attr); 779 kobject_put(ecryptfs_kobj);
867 sysfs_remove_file(&ecryptfs_subsys.kobj,
868 &sysfs_attr_version_str.attr);
869 subsystem_unregister(&ecryptfs_subsys);
870} 780}
871 781
872static int __init ecryptfs_init(void) 782static int __init ecryptfs_init(void)
@@ -894,7 +804,6 @@ static int __init ecryptfs_init(void)
894 printk(KERN_ERR "Failed to register filesystem\n"); 804 printk(KERN_ERR "Failed to register filesystem\n");
895 goto out_free_kmem_caches; 805 goto out_free_kmem_caches;
896 } 806 }
897 kobj_set_kset_s(&ecryptfs_subsys, fs_subsys);
898 rc = do_sysfs_registration(); 807 rc = do_sysfs_registration();
899 if (rc) { 808 if (rc) {
900 printk(KERN_ERR "sysfs registration failed\n"); 809 printk(KERN_ERR "sysfs registration failed\n");
diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index 84f9f7dfdf5b..e5e80d1a4687 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -744,9 +744,6 @@ static inline void unregister_fuseblk(void)
744} 744}
745#endif 745#endif
746 746
747static decl_subsys(fuse, NULL, NULL);
748static decl_subsys(connections, NULL, NULL);
749
750static void fuse_inode_init_once(struct kmem_cache *cachep, void *foo) 747static void fuse_inode_init_once(struct kmem_cache *cachep, void *foo)
751{ 748{
752 struct inode * inode = foo; 749 struct inode * inode = foo;
@@ -791,32 +788,37 @@ static void fuse_fs_cleanup(void)
791 kmem_cache_destroy(fuse_inode_cachep); 788 kmem_cache_destroy(fuse_inode_cachep);
792} 789}
793 790
791static struct kobject *fuse_kobj;
792static struct kobject *connections_kobj;
793
794static int fuse_sysfs_init(void) 794static int fuse_sysfs_init(void)
795{ 795{
796 int err; 796 int err;
797 797
798 kobj_set_kset_s(&fuse_subsys, fs_subsys); 798 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
799 err = subsystem_register(&fuse_subsys); 799 if (!fuse_kobj) {
800 if (err) 800 err = -ENOMEM;
801 goto out_err; 801 goto out_err;
802 }
802 803
803 kobj_set_kset_s(&connections_subsys, fuse_subsys); 804 connections_kobj = kobject_create_and_add("connections", fuse_kobj);
804 err = subsystem_register(&connections_subsys); 805 if (!connections_kobj) {
805 if (err) 806 err = -ENOMEM;
806 goto out_fuse_unregister; 807 goto out_fuse_unregister;
808 }
807 809
808 return 0; 810 return 0;
809 811
810 out_fuse_unregister: 812 out_fuse_unregister:
811 subsystem_unregister(&fuse_subsys); 813 kobject_put(fuse_kobj);
812 out_err: 814 out_err:
813 return err; 815 return err;
814} 816}
815 817
816static void fuse_sysfs_cleanup(void) 818static void fuse_sysfs_cleanup(void)
817{ 819{
818 subsystem_unregister(&connections_subsys); 820 kobject_put(connections_kobj);
819 subsystem_unregister(&fuse_subsys); 821 kobject_put(fuse_kobj);
820} 822}
821 823
822static int __init fuse_init(void) 824static int __init fuse_init(void)
diff --git a/fs/gfs2/locking/dlm/sysfs.c b/fs/gfs2/locking/dlm/sysfs.c
index ae9e6a25fe2b..a87b09839761 100644
--- a/fs/gfs2/locking/dlm/sysfs.c
+++ b/fs/gfs2/locking/dlm/sysfs.c
@@ -189,51 +189,39 @@ static struct kobj_type gdlm_ktype = {
189 .sysfs_ops = &gdlm_attr_ops, 189 .sysfs_ops = &gdlm_attr_ops,
190}; 190};
191 191
192static struct kset gdlm_kset = { 192static struct kset *gdlm_kset;
193 .ktype = &gdlm_ktype,
194};
195 193
196int gdlm_kobject_setup(struct gdlm_ls *ls, struct kobject *fskobj) 194int gdlm_kobject_setup(struct gdlm_ls *ls, struct kobject *fskobj)
197{ 195{
198 int error; 196 int error;
199 197
200 error = kobject_set_name(&ls->kobj, "%s", "lock_module"); 198 ls->kobj.kset = gdlm_kset;
201 if (error) { 199 error = kobject_init_and_add(&ls->kobj, &gdlm_ktype, fskobj,
202 log_error("can't set kobj name %d", error); 200 "lock_module");
203 return error;
204 }
205
206 ls->kobj.kset = &gdlm_kset;
207 ls->kobj.ktype = &gdlm_ktype;
208 ls->kobj.parent = fskobj;
209
210 error = kobject_register(&ls->kobj);
211 if (error) 201 if (error)
212 log_error("can't register kobj %d", error); 202 log_error("can't register kobj %d", error);
203 kobject_uevent(&ls->kobj, KOBJ_ADD);
213 204
214 return error; 205 return error;
215} 206}
216 207
217void gdlm_kobject_release(struct gdlm_ls *ls) 208void gdlm_kobject_release(struct gdlm_ls *ls)
218{ 209{
219 kobject_unregister(&ls->kobj); 210 kobject_put(&ls->kobj);
220} 211}
221 212
222int gdlm_sysfs_init(void) 213int gdlm_sysfs_init(void)
223{ 214{
224 int error; 215 gdlm_kset = kset_create_and_add("lock_dlm", NULL, kernel_kobj);
225 216 if (!gdlm_kset) {
226 kobject_set_name(&gdlm_kset.kobj, "lock_dlm"); 217 printk(KERN_WARNING "%s: can not create kset\n", __FUNCTION__);
227 kobj_set_kset_s(&gdlm_kset, kernel_subsys); 218 return -ENOMEM;
228 error = kset_register(&gdlm_kset); 219 }
229 if (error) 220 return 0;
230 printk("lock_dlm: cannot register kset %d\n", error);
231
232 return error;
233} 221}
234 222
235void gdlm_sysfs_exit(void) 223void gdlm_sysfs_exit(void)
236{ 224{
237 kset_unregister(&gdlm_kset); 225 kset_unregister(gdlm_kset);
238} 226}
239 227
diff --git a/fs/gfs2/sys.c b/fs/gfs2/sys.c
index 06e0b7768d97..3a3176b846f3 100644
--- a/fs/gfs2/sys.c
+++ b/fs/gfs2/sys.c
@@ -221,9 +221,7 @@ static struct kobj_type gfs2_ktype = {
221 .sysfs_ops = &gfs2_attr_ops, 221 .sysfs_ops = &gfs2_attr_ops,
222}; 222};
223 223
224static struct kset gfs2_kset = { 224static struct kset *gfs2_kset;
225 .ktype = &gfs2_ktype,
226};
227 225
228/* 226/*
229 * display struct lm_lockstruct fields 227 * display struct lm_lockstruct fields
@@ -495,14 +493,9 @@ int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
495{ 493{
496 int error; 494 int error;
497 495
498 sdp->sd_kobj.kset = &gfs2_kset; 496 sdp->sd_kobj.kset = gfs2_kset;
499 sdp->sd_kobj.ktype = &gfs2_ktype; 497 error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
500 498 "%s", sdp->sd_table_name);
501 error = kobject_set_name(&sdp->sd_kobj, "%s", sdp->sd_table_name);
502 if (error)
503 goto fail;
504
505 error = kobject_register(&sdp->sd_kobj);
506 if (error) 499 if (error)
507 goto fail; 500 goto fail;
508 501
@@ -522,6 +515,7 @@ int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
522 if (error) 515 if (error)
523 goto fail_args; 516 goto fail_args;
524 517
518 kobject_uevent(&sdp->sd_kobj, KOBJ_ADD);
525 return 0; 519 return 0;
526 520
527fail_args: 521fail_args:
@@ -531,7 +525,7 @@ fail_counters:
531fail_lockstruct: 525fail_lockstruct:
532 sysfs_remove_group(&sdp->sd_kobj, &lockstruct_group); 526 sysfs_remove_group(&sdp->sd_kobj, &lockstruct_group);
533fail_reg: 527fail_reg:
534 kobject_unregister(&sdp->sd_kobj); 528 kobject_put(&sdp->sd_kobj);
535fail: 529fail:
536 fs_err(sdp, "error %d adding sysfs files", error); 530 fs_err(sdp, "error %d adding sysfs files", error);
537 return error; 531 return error;
@@ -543,21 +537,22 @@ void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
543 sysfs_remove_group(&sdp->sd_kobj, &args_group); 537 sysfs_remove_group(&sdp->sd_kobj, &args_group);
544 sysfs_remove_group(&sdp->sd_kobj, &counters_group); 538 sysfs_remove_group(&sdp->sd_kobj, &counters_group);
545 sysfs_remove_group(&sdp->sd_kobj, &lockstruct_group); 539 sysfs_remove_group(&sdp->sd_kobj, &lockstruct_group);
546 kobject_unregister(&sdp->sd_kobj); 540 kobject_put(&sdp->sd_kobj);
547} 541}
548 542
549int gfs2_sys_init(void) 543int gfs2_sys_init(void)
550{ 544{
551 gfs2_sys_margs = NULL; 545 gfs2_sys_margs = NULL;
552 spin_lock_init(&gfs2_sys_margs_lock); 546 spin_lock_init(&gfs2_sys_margs_lock);
553 kobject_set_name(&gfs2_kset.kobj, "gfs2"); 547 gfs2_kset = kset_create_and_add("gfs2", NULL, fs_kobj);
554 kobj_set_kset_s(&gfs2_kset, fs_subsys); 548 if (!gfs2_kset)
555 return kset_register(&gfs2_kset); 549 return -ENOMEM;
550 return 0;
556} 551}
557 552
558void gfs2_sys_uninit(void) 553void gfs2_sys_uninit(void)
559{ 554{
560 kfree(gfs2_sys_margs); 555 kfree(gfs2_sys_margs);
561 kset_unregister(&gfs2_kset); 556 kset_unregister(gfs2_kset);
562} 557}
563 558
diff --git a/fs/namespace.c b/fs/namespace.c
index 06083885b21e..61bf376e29e8 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -41,8 +41,8 @@ static struct kmem_cache *mnt_cache __read_mostly;
41static struct rw_semaphore namespace_sem; 41static struct rw_semaphore namespace_sem;
42 42
43/* /sys/fs */ 43/* /sys/fs */
44decl_subsys(fs, NULL, NULL); 44struct kobject *fs_kobj;
45EXPORT_SYMBOL_GPL(fs_subsys); 45EXPORT_SYMBOL_GPL(fs_kobj);
46 46
47static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) 47static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
48{ 48{
@@ -1861,10 +1861,9 @@ void __init mnt_init(void)
1861 if (err) 1861 if (err)
1862 printk(KERN_WARNING "%s: sysfs_init error: %d\n", 1862 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
1863 __FUNCTION__, err); 1863 __FUNCTION__, err);
1864 err = subsystem_register(&fs_subsys); 1864 fs_kobj = kobject_create_and_add("fs", NULL);
1865 if (err) 1865 if (!fs_kobj)
1866 printk(KERN_WARNING "%s: subsystem_register error: %d\n", 1866 printk(KERN_WARNING "%s: kobj create error\n", __FUNCTION__);
1867 __FUNCTION__, err);
1868 init_rootfs(); 1867 init_rootfs();
1869 init_mount_tree(); 1868 init_mount_tree();
1870} 1869}
diff --git a/fs/ocfs2/cluster/masklog.c b/fs/ocfs2/cluster/masklog.c
index a4882c8df945..23c732f27529 100644
--- a/fs/ocfs2/cluster/masklog.c
+++ b/fs/ocfs2/cluster/masklog.c
@@ -146,7 +146,7 @@ static struct kset mlog_kset = {
146 .kobj = {.ktype = &mlog_ktype}, 146 .kobj = {.ktype = &mlog_ktype},
147}; 147};
148 148
149int mlog_sys_init(struct kset *o2cb_subsys) 149int mlog_sys_init(struct kset *o2cb_kset)
150{ 150{
151 int i = 0; 151 int i = 0;
152 152
@@ -157,7 +157,7 @@ int mlog_sys_init(struct kset *o2cb_subsys)
157 mlog_attr_ptrs[i] = NULL; 157 mlog_attr_ptrs[i] = NULL;
158 158
159 kobject_set_name(&mlog_kset.kobj, "logmask"); 159 kobject_set_name(&mlog_kset.kobj, "logmask");
160 kobj_set_kset_s(&mlog_kset, *o2cb_subsys); 160 mlog_kset.kobj.kset = o2cb_kset;
161 return kset_register(&mlog_kset); 161 return kset_register(&mlog_kset);
162} 162}
163 163
diff --git a/fs/ocfs2/cluster/sys.c b/fs/ocfs2/cluster/sys.c
index 64f6f378fd09..a4b07730b2e1 100644
--- a/fs/ocfs2/cluster/sys.c
+++ b/fs/ocfs2/cluster/sys.c
@@ -28,96 +28,55 @@
28#include <linux/module.h> 28#include <linux/module.h>
29#include <linux/kobject.h> 29#include <linux/kobject.h>
30#include <linux/sysfs.h> 30#include <linux/sysfs.h>
31#include <linux/fs.h>
31 32
32#include "ocfs2_nodemanager.h" 33#include "ocfs2_nodemanager.h"
33#include "masklog.h" 34#include "masklog.h"
34#include "sys.h" 35#include "sys.h"
35 36
36struct o2cb_attribute {
37 struct attribute attr;
38 ssize_t (*show)(char *buf);
39 ssize_t (*store)(const char *buf, size_t count);
40};
41
42#define O2CB_ATTR(_name, _mode, _show, _store) \
43struct o2cb_attribute o2cb_attr_##_name = __ATTR(_name, _mode, _show, _store)
44
45#define to_o2cb_attr(_attr) container_of(_attr, struct o2cb_attribute, attr)
46 37
47static ssize_t o2cb_interface_revision_show(char *buf) 38static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
39 char *buf)
48{ 40{
49 return snprintf(buf, PAGE_SIZE, "%u\n", O2NM_API_VERSION); 41 return snprintf(buf, PAGE_SIZE, "%u\n", O2NM_API_VERSION);
50} 42}
51 43static struct kobj_attribute attr_version =
52static O2CB_ATTR(interface_revision, S_IFREG | S_IRUGO, o2cb_interface_revision_show, NULL); 44 __ATTR(interface_revision, S_IFREG | S_IRUGO, version_show, NULL);
53 45
54static struct attribute *o2cb_attrs[] = { 46static struct attribute *o2cb_attrs[] = {
55 &o2cb_attr_interface_revision.attr, 47 &attr_version.attr,
56 NULL, 48 NULL,
57}; 49};
58 50
59static ssize_t 51static struct attribute_group o2cb_attr_group = {
60o2cb_show(struct kobject * kobj, struct attribute * attr, char * buffer); 52 .attrs = o2cb_attrs,
61static ssize_t
62o2cb_store(struct kobject * kobj, struct attribute * attr,
63 const char * buffer, size_t count);
64static struct sysfs_ops o2cb_sysfs_ops = {
65 .show = o2cb_show,
66 .store = o2cb_store,
67}; 53};
68 54
69static struct kobj_type o2cb_subsys_type = { 55static struct kset *o2cb_kset;
70 .default_attrs = o2cb_attrs,
71 .sysfs_ops = &o2cb_sysfs_ops,
72};
73
74/* gives us o2cb_subsys */
75static decl_subsys(o2cb, NULL, NULL);
76
77static ssize_t
78o2cb_show(struct kobject * kobj, struct attribute * attr, char * buffer)
79{
80 struct o2cb_attribute *o2cb_attr = to_o2cb_attr(attr);
81 struct kset *sbs = to_kset(kobj);
82
83 BUG_ON(sbs != &o2cb_subsys);
84
85 if (o2cb_attr->show)
86 return o2cb_attr->show(buffer);
87 return -EIO;
88}
89
90static ssize_t
91o2cb_store(struct kobject * kobj, struct attribute * attr,
92 const char * buffer, size_t count)
93{
94 struct o2cb_attribute *o2cb_attr = to_o2cb_attr(attr);
95 struct kset *sbs = to_kset(kobj);
96
97 BUG_ON(sbs != &o2cb_subsys);
98
99 if (o2cb_attr->store)
100 return o2cb_attr->store(buffer, count);
101 return -EIO;
102}
103 56
104void o2cb_sys_shutdown(void) 57void o2cb_sys_shutdown(void)
105{ 58{
106 mlog_sys_shutdown(); 59 mlog_sys_shutdown();
107 subsystem_unregister(&o2cb_subsys); 60 kset_unregister(o2cb_kset);
108} 61}
109 62
110int o2cb_sys_init(void) 63int o2cb_sys_init(void)
111{ 64{
112 int ret; 65 int ret;
113 66
114 o2cb_subsys.kobj.ktype = &o2cb_subsys_type; 67 o2cb_kset = kset_create_and_add("o2cb", NULL, fs_kobj);
115 ret = subsystem_register(&o2cb_subsys); 68 if (!o2cb_kset)
69 return -ENOMEM;
70
71 ret = sysfs_create_group(&o2cb_kset->kobj, &o2cb_attr_group);
116 if (ret) 72 if (ret)
117 return ret; 73 goto error;
118 74
119 ret = mlog_sys_init(&o2cb_subsys); 75 ret = mlog_sys_init(o2cb_kset);
120 if (ret) 76 if (ret)
121 subsystem_unregister(&o2cb_subsys); 77 goto error;
78 return 0;
79error:
80 kset_unregister(o2cb_kset);
122 return ret; 81 return ret;
123} 82}
diff --git a/fs/partitions/check.c b/fs/partitions/check.c
index 722e12e5acc7..739da701ae7b 100644
--- a/fs/partitions/check.c
+++ b/fs/partitions/check.c
@@ -195,96 +195,45 @@ check_partition(struct gendisk *hd, struct block_device *bdev)
195 return ERR_PTR(res); 195 return ERR_PTR(res);
196} 196}
197 197
198/* 198static ssize_t part_start_show(struct device *dev,
199 * sysfs bindings for partitions 199 struct device_attribute *attr, char *buf)
200 */
201
202struct part_attribute {
203 struct attribute attr;
204 ssize_t (*show)(struct hd_struct *,char *);
205 ssize_t (*store)(struct hd_struct *,const char *, size_t);
206};
207
208static ssize_t
209part_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
210{ 200{
211 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); 201 struct hd_struct *p = dev_to_part(dev);
212 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
213 ssize_t ret = 0;
214 if (part_attr->show)
215 ret = part_attr->show(p, page);
216 return ret;
217}
218static ssize_t
219part_attr_store(struct kobject * kobj, struct attribute * attr,
220 const char *page, size_t count)
221{
222 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
223 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
224 ssize_t ret = 0;
225 202
226 if (part_attr->store) 203 return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
227 ret = part_attr->store(p, page, count);
228 return ret;
229} 204}
230 205
231static struct sysfs_ops part_sysfs_ops = { 206static ssize_t part_size_show(struct device *dev,
232 .show = part_attr_show, 207 struct device_attribute *attr, char *buf)
233 .store = part_attr_store,
234};
235
236static ssize_t part_uevent_store(struct hd_struct * p,
237 const char *page, size_t count)
238{ 208{
239 kobject_uevent(&p->kobj, KOBJ_ADD); 209 struct hd_struct *p = dev_to_part(dev);
240 return count; 210 return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
241} 211}
242static ssize_t part_dev_read(struct hd_struct * p, char *page) 212
243{ 213static ssize_t part_stat_show(struct device *dev,
244 struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj); 214 struct device_attribute *attr, char *buf)
245 dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno);
246 return print_dev_t(page, dev);
247}
248static ssize_t part_start_read(struct hd_struct * p, char *page)
249{
250 return sprintf(page, "%llu\n",(unsigned long long)p->start_sect);
251}
252static ssize_t part_size_read(struct hd_struct * p, char *page)
253{
254 return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects);
255}
256static ssize_t part_stat_read(struct hd_struct * p, char *page)
257{ 215{
258 return sprintf(page, "%8u %8llu %8u %8llu\n", 216 struct hd_struct *p = dev_to_part(dev);
217
218 return sprintf(buf, "%8u %8llu %8u %8llu\n",
259 p->ios[0], (unsigned long long)p->sectors[0], 219 p->ios[0], (unsigned long long)p->sectors[0],
260 p->ios[1], (unsigned long long)p->sectors[1]); 220 p->ios[1], (unsigned long long)p->sectors[1]);
261} 221}
262static struct part_attribute part_attr_uevent = {
263 .attr = {.name = "uevent", .mode = S_IWUSR },
264 .store = part_uevent_store
265};
266static struct part_attribute part_attr_dev = {
267 .attr = {.name = "dev", .mode = S_IRUGO },
268 .show = part_dev_read
269};
270static struct part_attribute part_attr_start = {
271 .attr = {.name = "start", .mode = S_IRUGO },
272 .show = part_start_read
273};
274static struct part_attribute part_attr_size = {
275 .attr = {.name = "size", .mode = S_IRUGO },
276 .show = part_size_read
277};
278static struct part_attribute part_attr_stat = {
279 .attr = {.name = "stat", .mode = S_IRUGO },
280 .show = part_stat_read
281};
282 222
283#ifdef CONFIG_FAIL_MAKE_REQUEST 223#ifdef CONFIG_FAIL_MAKE_REQUEST
224static ssize_t part_fail_show(struct device *dev,
225 struct device_attribute *attr, char *buf)
226{
227 struct hd_struct *p = dev_to_part(dev);
284 228
285static ssize_t part_fail_store(struct hd_struct * p, 229 return sprintf(buf, "%d\n", p->make_it_fail);
230}
231
232static ssize_t part_fail_store(struct device *dev,
233 struct device_attribute *attr,
286 const char *buf, size_t count) 234 const char *buf, size_t count)
287{ 235{
236 struct hd_struct *p = dev_to_part(dev);
288 int i; 237 int i;
289 238
290 if (count > 0 && sscanf(buf, "%d", &i) > 0) 239 if (count > 0 && sscanf(buf, "%d", &i) > 0)
@@ -292,50 +241,53 @@ static ssize_t part_fail_store(struct hd_struct * p,
292 241
293 return count; 242 return count;
294} 243}
295static ssize_t part_fail_read(struct hd_struct * p, char *page) 244#endif
296{
297 return sprintf(page, "%d\n", p->make_it_fail);
298}
299static struct part_attribute part_attr_fail = {
300 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
301 .store = part_fail_store,
302 .show = part_fail_read
303};
304 245
246static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
247static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
248static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
249#ifdef CONFIG_FAIL_MAKE_REQUEST
250static struct device_attribute dev_attr_fail =
251 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
305#endif 252#endif
306 253
307static struct attribute * default_attrs[] = { 254static struct attribute *part_attrs[] = {
308 &part_attr_uevent.attr, 255 &dev_attr_start.attr,
309 &part_attr_dev.attr, 256 &dev_attr_size.attr,
310 &part_attr_start.attr, 257 &dev_attr_stat.attr,
311 &part_attr_size.attr,
312 &part_attr_stat.attr,
313#ifdef CONFIG_FAIL_MAKE_REQUEST 258#ifdef CONFIG_FAIL_MAKE_REQUEST
314 &part_attr_fail.attr, 259 &dev_attr_fail.attr,
315#endif 260#endif
316 NULL, 261 NULL
317}; 262};
318 263
319extern struct kset block_subsys; 264static struct attribute_group part_attr_group = {
265 .attrs = part_attrs,
266};
320 267
321static void part_release(struct kobject *kobj) 268static struct attribute_group *part_attr_groups[] = {
269 &part_attr_group,
270 NULL
271};
272
273static void part_release(struct device *dev)
322{ 274{
323 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); 275 struct hd_struct *p = dev_to_part(dev);
324 kfree(p); 276 kfree(p);
325} 277}
326 278
327struct kobj_type ktype_part = { 279struct device_type part_type = {
280 .name = "partition",
281 .groups = part_attr_groups,
328 .release = part_release, 282 .release = part_release,
329 .default_attrs = default_attrs,
330 .sysfs_ops = &part_sysfs_ops,
331}; 283};
332 284
333static inline void partition_sysfs_add_subdir(struct hd_struct *p) 285static inline void partition_sysfs_add_subdir(struct hd_struct *p)
334{ 286{
335 struct kobject *k; 287 struct kobject *k;
336 288
337 k = kobject_get(&p->kobj); 289 k = kobject_get(&p->dev.kobj);
338 p->holder_dir = kobject_add_dir(k, "holders"); 290 p->holder_dir = kobject_create_and_add("holders", k);
339 kobject_put(k); 291 kobject_put(k);
340} 292}
341 293
@@ -343,15 +295,16 @@ static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
343{ 295{
344 struct kobject *k; 296 struct kobject *k;
345 297
346 k = kobject_get(&disk->kobj); 298 k = kobject_get(&disk->dev.kobj);
347 disk->holder_dir = kobject_add_dir(k, "holders"); 299 disk->holder_dir = kobject_create_and_add("holders", k);
348 disk->slave_dir = kobject_add_dir(k, "slaves"); 300 disk->slave_dir = kobject_create_and_add("slaves", k);
349 kobject_put(k); 301 kobject_put(k);
350} 302}
351 303
352void delete_partition(struct gendisk *disk, int part) 304void delete_partition(struct gendisk *disk, int part)
353{ 305{
354 struct hd_struct *p = disk->part[part-1]; 306 struct hd_struct *p = disk->part[part-1];
307
355 if (!p) 308 if (!p)
356 return; 309 return;
357 if (!p->nr_sects) 310 if (!p->nr_sects)
@@ -361,113 +314,55 @@ void delete_partition(struct gendisk *disk, int part)
361 p->nr_sects = 0; 314 p->nr_sects = 0;
362 p->ios[0] = p->ios[1] = 0; 315 p->ios[0] = p->ios[1] = 0;
363 p->sectors[0] = p->sectors[1] = 0; 316 p->sectors[0] = p->sectors[1] = 0;
364 sysfs_remove_link(&p->kobj, "subsystem"); 317 kobject_put(p->holder_dir);
365 kobject_unregister(p->holder_dir); 318 device_del(&p->dev);
366 kobject_uevent(&p->kobj, KOBJ_REMOVE); 319 put_device(&p->dev);
367 kobject_del(&p->kobj);
368 kobject_put(&p->kobj);
369} 320}
370 321
371void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags) 322void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags)
372{ 323{
373 struct hd_struct *p; 324 struct hd_struct *p;
325 int err;
374 326
375 p = kzalloc(sizeof(*p), GFP_KERNEL); 327 p = kzalloc(sizeof(*p), GFP_KERNEL);
376 if (!p) 328 if (!p)
377 return; 329 return;
378 330
379 p->start_sect = start; 331 p->start_sect = start;
380 p->nr_sects = len; 332 p->nr_sects = len;
381 p->partno = part; 333 p->partno = part;
382 p->policy = disk->policy; 334 p->policy = disk->policy;
383 335
384 if (isdigit(disk->kobj.k_name[strlen(disk->kobj.k_name)-1])) 336 if (isdigit(disk->dev.bus_id[strlen(disk->dev.bus_id)-1]))
385 kobject_set_name(&p->kobj, "%sp%d", 337 snprintf(p->dev.bus_id, BUS_ID_SIZE,
386 kobject_name(&disk->kobj), part); 338 "%sp%d", disk->dev.bus_id, part);
387 else 339 else
388 kobject_set_name(&p->kobj, "%s%d", 340 snprintf(p->dev.bus_id, BUS_ID_SIZE,
389 kobject_name(&disk->kobj),part); 341 "%s%d", disk->dev.bus_id, part);
390 p->kobj.parent = &disk->kobj; 342
391 p->kobj.ktype = &ktype_part; 343 device_initialize(&p->dev);
392 kobject_init(&p->kobj); 344 p->dev.devt = MKDEV(disk->major, disk->first_minor + part);
393 kobject_add(&p->kobj); 345 p->dev.class = &block_class;
394 if (!disk->part_uevent_suppress) 346 p->dev.type = &part_type;
395 kobject_uevent(&p->kobj, KOBJ_ADD); 347 p->dev.parent = &disk->dev;
396 sysfs_create_link(&p->kobj, &block_subsys.kobj, "subsystem"); 348 disk->part[part-1] = p;
349
350 /* delay uevent until 'holders' subdir is created */
351 p->dev.uevent_suppress = 1;
352 device_add(&p->dev);
353 partition_sysfs_add_subdir(p);
354 p->dev.uevent_suppress = 0;
397 if (flags & ADDPART_FLAG_WHOLEDISK) { 355 if (flags & ADDPART_FLAG_WHOLEDISK) {
398 static struct attribute addpartattr = { 356 static struct attribute addpartattr = {
399 .name = "whole_disk", 357 .name = "whole_disk",
400 .mode = S_IRUSR | S_IRGRP | S_IROTH, 358 .mode = S_IRUSR | S_IRGRP | S_IROTH,
401 }; 359 };
402 360 err = sysfs_create_file(&p->dev.kobj, &addpartattr);
403 sysfs_create_file(&p->kobj, &addpartattr);
404 } 361 }
405 partition_sysfs_add_subdir(p);
406 disk->part[part-1] = p;
407}
408 362
409static char *make_block_name(struct gendisk *disk) 363 /* suppress uevent if the disk supresses it */
410{ 364 if (!disk->dev.uevent_suppress)
411 char *name; 365 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
412 static char *block_str = "block:";
413 int size;
414 char *s;
415
416 size = strlen(block_str) + strlen(disk->disk_name) + 1;
417 name = kmalloc(size, GFP_KERNEL);
418 if (!name)
419 return NULL;
420 strcpy(name, block_str);
421 strcat(name, disk->disk_name);
422 /* ewww... some of these buggers have / in name... */
423 s = strchr(name, '/');
424 if (s)
425 *s = '!';
426 return name;
427}
428
429static int disk_sysfs_symlinks(struct gendisk *disk)
430{
431 struct device *target = get_device(disk->driverfs_dev);
432 int err;
433 char *disk_name = NULL;
434
435 if (target) {
436 disk_name = make_block_name(disk);
437 if (!disk_name) {
438 err = -ENOMEM;
439 goto err_out;
440 }
441
442 err = sysfs_create_link(&disk->kobj, &target->kobj, "device");
443 if (err)
444 goto err_out_disk_name;
445
446 err = sysfs_create_link(&target->kobj, &disk->kobj, disk_name);
447 if (err)
448 goto err_out_dev_link;
449 }
450
451 err = sysfs_create_link(&disk->kobj, &block_subsys.kobj,
452 "subsystem");
453 if (err)
454 goto err_out_disk_name_lnk;
455
456 kfree(disk_name);
457
458 return 0;
459
460err_out_disk_name_lnk:
461 if (target) {
462 sysfs_remove_link(&target->kobj, disk_name);
463err_out_dev_link:
464 sysfs_remove_link(&disk->kobj, "device");
465err_out_disk_name:
466 kfree(disk_name);
467err_out:
468 put_device(target);
469 }
470 return err;
471} 366}
472 367
473/* Not exported, helper to add_disk(). */ 368/* Not exported, helper to add_disk(). */
@@ -479,19 +374,29 @@ void register_disk(struct gendisk *disk)
479 struct hd_struct *p; 374 struct hd_struct *p;
480 int err; 375 int err;
481 376
482 kobject_set_name(&disk->kobj, "%s", disk->disk_name); 377 disk->dev.parent = disk->driverfs_dev;
483 /* ewww... some of these buggers have / in name... */ 378 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
484 s = strchr(disk->kobj.k_name, '/'); 379
380 strlcpy(disk->dev.bus_id, disk->disk_name, KOBJ_NAME_LEN);
381 /* ewww... some of these buggers have / in the name... */
382 s = strchr(disk->dev.bus_id, '/');
485 if (s) 383 if (s)
486 *s = '!'; 384 *s = '!';
487 if ((err = kobject_add(&disk->kobj))) 385
386 /* delay uevents, until we scanned partition table */
387 disk->dev.uevent_suppress = 1;
388
389 if (device_add(&disk->dev))
488 return; 390 return;
489 err = disk_sysfs_symlinks(disk); 391#ifndef CONFIG_SYSFS_DEPRECATED
392 err = sysfs_create_link(block_depr, &disk->dev.kobj,
393 kobject_name(&disk->dev.kobj));
490 if (err) { 394 if (err) {
491 kobject_del(&disk->kobj); 395 device_del(&disk->dev);
492 return; 396 return;
493 } 397 }
494 disk_sysfs_add_subdirs(disk); 398#endif
399 disk_sysfs_add_subdirs(disk);
495 400
496 /* No minors to use for partitions */ 401 /* No minors to use for partitions */
497 if (disk->minors == 1) 402 if (disk->minors == 1)
@@ -505,25 +410,23 @@ void register_disk(struct gendisk *disk)
505 if (!bdev) 410 if (!bdev)
506 goto exit; 411 goto exit;
507 412
508 /* scan partition table, but suppress uevents */
509 bdev->bd_invalidated = 1; 413 bdev->bd_invalidated = 1;
510 disk->part_uevent_suppress = 1;
511 err = blkdev_get(bdev, FMODE_READ, 0); 414 err = blkdev_get(bdev, FMODE_READ, 0);
512 disk->part_uevent_suppress = 0;
513 if (err < 0) 415 if (err < 0)
514 goto exit; 416 goto exit;
515 blkdev_put(bdev); 417 blkdev_put(bdev);
516 418
517exit: 419exit:
518 /* announce disk after possible partitions are already created */ 420 /* announce disk after possible partitions are created */
519 kobject_uevent(&disk->kobj, KOBJ_ADD); 421 disk->dev.uevent_suppress = 0;
422 kobject_uevent(&disk->dev.kobj, KOBJ_ADD);
520 423
521 /* announce possible partitions */ 424 /* announce possible partitions */
522 for (i = 1; i < disk->minors; i++) { 425 for (i = 1; i < disk->minors; i++) {
523 p = disk->part[i-1]; 426 p = disk->part[i-1];
524 if (!p || !p->nr_sects) 427 if (!p || !p->nr_sects)
525 continue; 428 continue;
526 kobject_uevent(&p->kobj, KOBJ_ADD); 429 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
527 } 430 }
528} 431}
529 432
@@ -602,19 +505,11 @@ void del_gendisk(struct gendisk *disk)
602 disk_stat_set_all(disk, 0); 505 disk_stat_set_all(disk, 0);
603 disk->stamp = 0; 506 disk->stamp = 0;
604 507
605 kobject_uevent(&disk->kobj, KOBJ_REMOVE); 508 kobject_put(disk->holder_dir);
606 kobject_unregister(disk->holder_dir); 509 kobject_put(disk->slave_dir);
607 kobject_unregister(disk->slave_dir); 510 disk->driverfs_dev = NULL;
608 if (disk->driverfs_dev) { 511#ifndef CONFIG_SYSFS_DEPRECATED
609 char *disk_name = make_block_name(disk); 512 sysfs_remove_link(block_depr, disk->dev.bus_id);
610 sysfs_remove_link(&disk->kobj, "device"); 513#endif
611 if (disk_name) { 514 device_del(&disk->dev);
612 sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name);
613 kfree(disk_name);
614 }
615 put_device(disk->driverfs_dev);
616 disk->driverfs_dev = NULL;
617 }
618 sysfs_remove_link(&disk->kobj, "subsystem");
619 kobject_del(&disk->kobj);
620} 515}
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index f281cc6584b0..4948d9bc405d 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -440,7 +440,7 @@ int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
440/** 440/**
441 * sysfs_remove_one - remove sysfs_dirent from parent 441 * sysfs_remove_one - remove sysfs_dirent from parent
442 * @acxt: addrm context to use 442 * @acxt: addrm context to use
443 * @sd: sysfs_dirent to be added 443 * @sd: sysfs_dirent to be removed
444 * 444 *
445 * Mark @sd removed and drop nlink of parent inode if @sd is a 445 * Mark @sd removed and drop nlink of parent inode if @sd is a
446 * directory. @sd is unlinked from the children list. 446 * directory. @sd is unlinked from the children list.
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 4045bdcc4b33..8acf82bba44c 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -20,43 +20,6 @@
20 20
21#include "sysfs.h" 21#include "sysfs.h"
22 22
23#define to_sattr(a) container_of(a,struct subsys_attribute, attr)
24
25/*
26 * Subsystem file operations.
27 * These operations allow subsystems to have files that can be
28 * read/written.
29 */
30static ssize_t
31subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
32{
33 struct kset *kset = to_kset(kobj);
34 struct subsys_attribute * sattr = to_sattr(attr);
35 ssize_t ret = -EIO;
36
37 if (sattr->show)
38 ret = sattr->show(kset, page);
39 return ret;
40}
41
42static ssize_t
43subsys_attr_store(struct kobject * kobj, struct attribute * attr,
44 const char * page, size_t count)
45{
46 struct kset *kset = to_kset(kobj);
47 struct subsys_attribute * sattr = to_sattr(attr);
48 ssize_t ret = -EIO;
49
50 if (sattr->store)
51 ret = sattr->store(kset, page, count);
52 return ret;
53}
54
55static struct sysfs_ops subsys_sysfs_ops = {
56 .show = subsys_attr_show,
57 .store = subsys_attr_store,
58};
59
60/* 23/*
61 * There's one sysfs_buffer for each open file and one 24 * There's one sysfs_buffer for each open file and one
62 * sysfs_open_dirent for each sysfs_dirent with one or more open 25 * sysfs_open_dirent for each sysfs_dirent with one or more open
@@ -66,7 +29,7 @@ static struct sysfs_ops subsys_sysfs_ops = {
66 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open 29 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
67 * is protected by sysfs_open_dirent_lock. 30 * is protected by sysfs_open_dirent_lock.
68 */ 31 */
69static spinlock_t sysfs_open_dirent_lock = SPIN_LOCK_UNLOCKED; 32static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
70 33
71struct sysfs_open_dirent { 34struct sysfs_open_dirent {
72 atomic_t refcnt; 35 atomic_t refcnt;
@@ -354,31 +317,23 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
354{ 317{
355 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; 318 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
356 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; 319 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
357 struct sysfs_buffer * buffer; 320 struct sysfs_buffer *buffer;
358 struct sysfs_ops * ops = NULL; 321 struct sysfs_ops *ops;
359 int error; 322 int error = -EACCES;
360 323
361 /* need attr_sd for attr and ops, its parent for kobj */ 324 /* need attr_sd for attr and ops, its parent for kobj */
362 if (!sysfs_get_active_two(attr_sd)) 325 if (!sysfs_get_active_two(attr_sd))
363 return -ENODEV; 326 return -ENODEV;
364 327
365 /* if the kobject has no ktype, then we assume that it is a subsystem 328 /* every kobject with an attribute needs a ktype assigned */
366 * itself, and use ops for it. 329 if (kobj->ktype && kobj->ktype->sysfs_ops)
367 */
368 if (kobj->kset && kobj->kset->ktype)
369 ops = kobj->kset->ktype->sysfs_ops;
370 else if (kobj->ktype)
371 ops = kobj->ktype->sysfs_ops; 330 ops = kobj->ktype->sysfs_ops;
372 else 331 else {
373 ops = &subsys_sysfs_ops; 332 printk(KERN_ERR "missing sysfs attribute operations for "
374 333 "kobject: %s\n", kobject_name(kobj));
375 error = -EACCES; 334 WARN_ON(1);
376
377 /* No sysfs operations, either from having no subsystem,
378 * or the subsystem have no operations.
379 */
380 if (!ops)
381 goto err_out; 335 goto err_out;
336 }
382 337
383 /* File needs write support. 338 /* File needs write support.
384 * The inode's perms must say it's ok, 339 * The inode's perms must say it's ok,
diff --git a/fs/sysfs/symlink.c b/fs/sysfs/symlink.c
index 3eac20c63c41..5f66c4466151 100644
--- a/fs/sysfs/symlink.c
+++ b/fs/sysfs/symlink.c
@@ -19,39 +19,6 @@
19 19
20#include "sysfs.h" 20#include "sysfs.h"
21 21
22static int object_depth(struct sysfs_dirent *sd)
23{
24 int depth = 0;
25
26 for (; sd->s_parent; sd = sd->s_parent)
27 depth++;
28
29 return depth;
30}
31
32static int object_path_length(struct sysfs_dirent * sd)
33{
34 int length = 1;
35
36 for (; sd->s_parent; sd = sd->s_parent)
37 length += strlen(sd->s_name) + 1;
38
39 return length;
40}
41
42static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
43{
44 --length;
45 for (; sd->s_parent; sd = sd->s_parent) {
46 int cur = strlen(sd->s_name);
47
48 /* back up enough to print this bus id with '/' */
49 length -= cur;
50 strncpy(buffer + length, sd->s_name, cur);
51 *(buffer + --length) = '/';
52 }
53}
54
55/** 22/**
56 * sysfs_create_link - create symlink between two objects. 23 * sysfs_create_link - create symlink between two objects.
57 * @kobj: object whose directory we're creating the link in. 24 * @kobj: object whose directory we're creating the link in.
@@ -112,7 +79,6 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
112 return error; 79 return error;
113} 80}
114 81
115
116/** 82/**
117 * sysfs_remove_link - remove symlink in object's directory. 83 * sysfs_remove_link - remove symlink in object's directory.
118 * @kobj: object we're acting for. 84 * @kobj: object we're acting for.
@@ -124,24 +90,54 @@ void sysfs_remove_link(struct kobject * kobj, const char * name)
124 sysfs_hash_and_remove(kobj->sd, name); 90 sysfs_hash_and_remove(kobj->sd, name);
125} 91}
126 92
127static int sysfs_get_target_path(struct sysfs_dirent * parent_sd, 93static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
128 struct sysfs_dirent * target_sd, char *path) 94 struct sysfs_dirent *target_sd, char *path)
129{ 95{
130 char * s; 96 struct sysfs_dirent *base, *sd;
131 int depth, size; 97 char *s = path;
98 int len = 0;
99
100 /* go up to the root, stop at the base */
101 base = parent_sd;
102 while (base->s_parent) {
103 sd = target_sd->s_parent;
104 while (sd->s_parent && base != sd)
105 sd = sd->s_parent;
106
107 if (base == sd)
108 break;
109
110 strcpy(s, "../");
111 s += 3;
112 base = base->s_parent;
113 }
114
115 /* determine end of target string for reverse fillup */
116 sd = target_sd;
117 while (sd->s_parent && sd != base) {
118 len += strlen(sd->s_name) + 1;
119 sd = sd->s_parent;
120 }
132 121
133 depth = object_depth(parent_sd); 122 /* check limits */
134 size = object_path_length(target_sd) + depth * 3 - 1; 123 if (len < 2)
135 if (size > PATH_MAX) 124 return -EINVAL;
125 len--;
126 if ((s - path) + len > PATH_MAX)
136 return -ENAMETOOLONG; 127 return -ENAMETOOLONG;
137 128
138 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size); 129 /* reverse fillup of target string from target to base */
130 sd = target_sd;
131 while (sd->s_parent && sd != base) {
132 int slen = strlen(sd->s_name);
139 133
140 for (s = path; depth--; s += 3) 134 len -= slen;
141 strcpy(s,"../"); 135 strncpy(s + len, sd->s_name, slen);
136 if (len)
137 s[--len] = '/';
142 138
143 fill_object_path(target_sd, path, size); 139 sd = sd->s_parent;
144 pr_debug("%s: path = '%s'\n", __FUNCTION__, path); 140 }
145 141
146 return 0; 142 return 0;
147} 143}
diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h
index 7b74b60a68a4..fb7171b1bd22 100644
--- a/include/acpi/acpi_bus.h
+++ b/include/acpi/acpi_bus.h
@@ -319,7 +319,7 @@ struct acpi_bus_event {
319 u32 data; 319 u32 data;
320}; 320};
321 321
322extern struct kset acpi_subsys; 322extern struct kobject *acpi_kobj;
323extern int acpi_bus_generate_netlink_event(const char*, const char*, u8, int); 323extern int acpi_bus_generate_netlink_event(const char*, const char*, u8, int);
324/* 324/*
325 * External Functions 325 * External Functions
diff --git a/include/linux/device.h b/include/linux/device.h
index 2e15822fe409..1880208964d6 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -25,75 +25,69 @@
25#include <asm/device.h> 25#include <asm/device.h>
26 26
27#define DEVICE_NAME_SIZE 50 27#define DEVICE_NAME_SIZE 50
28#define DEVICE_NAME_HALF __stringify(20) /* Less than half to accommodate slop */ 28/* DEVICE_NAME_HALF is really less than half to accommodate slop */
29#define DEVICE_NAME_HALF __stringify(20)
29#define DEVICE_ID_SIZE 32 30#define DEVICE_ID_SIZE 32
30#define BUS_ID_SIZE KOBJ_NAME_LEN 31#define BUS_ID_SIZE KOBJ_NAME_LEN
31 32
32 33
33struct device; 34struct device;
34struct device_driver; 35struct device_driver;
36struct driver_private;
35struct class; 37struct class;
36struct class_device; 38struct class_device;
37struct bus_type; 39struct bus_type;
40struct bus_type_private;
38 41
39struct bus_attribute { 42struct bus_attribute {
40 struct attribute attr; 43 struct attribute attr;
41 ssize_t (*show)(struct bus_type *, char * buf); 44 ssize_t (*show)(struct bus_type *bus, char *buf);
42 ssize_t (*store)(struct bus_type *, const char * buf, size_t count); 45 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
43}; 46};
44 47
45#define BUS_ATTR(_name,_mode,_show,_store) \ 48#define BUS_ATTR(_name, _mode, _show, _store) \
46struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store) 49struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
47 50
48extern int __must_check bus_create_file(struct bus_type *, 51extern int __must_check bus_create_file(struct bus_type *,
49 struct bus_attribute *); 52 struct bus_attribute *);
50extern void bus_remove_file(struct bus_type *, struct bus_attribute *); 53extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
51 54
52struct bus_type { 55struct bus_type {
53 const char * name; 56 const char *name;
54 struct module * owner; 57 struct bus_attribute *bus_attrs;
58 struct device_attribute *dev_attrs;
59 struct driver_attribute *drv_attrs;
55 60
56 struct kset subsys; 61 int (*match)(struct device *dev, struct device_driver *drv);
57 struct kset drivers; 62 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
58 struct kset devices; 63 int (*probe)(struct device *dev);
59 struct klist klist_devices; 64 int (*remove)(struct device *dev);
60 struct klist klist_drivers; 65 void (*shutdown)(struct device *dev);
61
62 struct blocking_notifier_head bus_notifier;
63
64 struct bus_attribute * bus_attrs;
65 struct device_attribute * dev_attrs;
66 struct driver_attribute * drv_attrs;
67
68 int (*match)(struct device * dev, struct device_driver * drv);
69 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
70 int (*probe)(struct device * dev);
71 int (*remove)(struct device * dev);
72 void (*shutdown)(struct device * dev);
73 66
74 int (*suspend)(struct device * dev, pm_message_t state); 67 int (*suspend)(struct device *dev, pm_message_t state);
75 int (*suspend_late)(struct device * dev, pm_message_t state); 68 int (*suspend_late)(struct device *dev, pm_message_t state);
76 int (*resume_early)(struct device * dev); 69 int (*resume_early)(struct device *dev);
77 int (*resume)(struct device * dev); 70 int (*resume)(struct device *dev);
78 71
79 unsigned int drivers_autoprobe:1; 72 struct bus_type_private *p;
80}; 73};
81 74
82extern int __must_check bus_register(struct bus_type * bus); 75extern int __must_check bus_register(struct bus_type *bus);
83extern void bus_unregister(struct bus_type * bus); 76extern void bus_unregister(struct bus_type *bus);
84 77
85extern int __must_check bus_rescan_devices(struct bus_type * bus); 78extern int __must_check bus_rescan_devices(struct bus_type *bus);
86 79
87/* iterator helpers for buses */ 80/* iterator helpers for buses */
88 81
89int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data, 82int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
90 int (*fn)(struct device *, void *)); 83 int (*fn)(struct device *dev, void *data));
91struct device * bus_find_device(struct bus_type *bus, struct device *start, 84struct device *bus_find_device(struct bus_type *bus, struct device *start,
92 void *data, int (*match)(struct device *, void *)); 85 void *data,
86 int (*match)(struct device *dev, void *data));
93 87
94int __must_check bus_for_each_drv(struct bus_type *bus, 88int __must_check bus_for_each_drv(struct bus_type *bus,
95 struct device_driver *start, void *data, 89 struct device_driver *start, void *data,
96 int (*fn)(struct device_driver *, void *)); 90 int (*fn)(struct device_driver *, void *));
97 91
98/* 92/*
99 * Bus notifiers: Get notified of addition/removal of devices 93 * Bus notifiers: Get notified of addition/removal of devices
@@ -118,111 +112,128 @@ extern int bus_unregister_notifier(struct bus_type *bus,
118#define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be 112#define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
119 unbound */ 113 unbound */
120 114
115extern struct kset *bus_get_kset(struct bus_type *bus);
116extern struct klist *bus_get_device_klist(struct bus_type *bus);
117
121struct device_driver { 118struct device_driver {
122 const char * name; 119 const char *name;
123 struct bus_type * bus; 120 struct bus_type *bus;
124 121
125 struct kobject kobj; 122 struct module *owner;
126 struct klist klist_devices; 123 const char *mod_name; /* used for built-in modules */
127 struct klist_node knode_bus;
128 124
129 struct module * owner; 125 int (*probe) (struct device *dev);
130 const char * mod_name; /* used for built-in modules */ 126 int (*remove) (struct device *dev);
131 struct module_kobject * mkobj; 127 void (*shutdown) (struct device *dev);
128 int (*suspend) (struct device *dev, pm_message_t state);
129 int (*resume) (struct device *dev);
130 struct attribute_group **groups;
132 131
133 int (*probe) (struct device * dev); 132 struct driver_private *p;
134 int (*remove) (struct device * dev);
135 void (*shutdown) (struct device * dev);
136 int (*suspend) (struct device * dev, pm_message_t state);
137 int (*resume) (struct device * dev);
138}; 133};
139 134
140 135
141extern int __must_check driver_register(struct device_driver * drv); 136extern int __must_check driver_register(struct device_driver *drv);
142extern void driver_unregister(struct device_driver * drv); 137extern void driver_unregister(struct device_driver *drv);
143 138
144extern struct device_driver * get_driver(struct device_driver * drv); 139extern struct device_driver *get_driver(struct device_driver *drv);
145extern void put_driver(struct device_driver * drv); 140extern void put_driver(struct device_driver *drv);
146extern struct device_driver *driver_find(const char *name, struct bus_type *bus); 141extern struct device_driver *driver_find(const char *name,
142 struct bus_type *bus);
147extern int driver_probe_done(void); 143extern int driver_probe_done(void);
148 144
149/* sysfs interface for exporting driver attributes */ 145/* sysfs interface for exporting driver attributes */
150 146
151struct driver_attribute { 147struct driver_attribute {
152 struct attribute attr; 148 struct attribute attr;
153 ssize_t (*show)(struct device_driver *, char * buf); 149 ssize_t (*show)(struct device_driver *driver, char *buf);
154 ssize_t (*store)(struct device_driver *, const char * buf, size_t count); 150 ssize_t (*store)(struct device_driver *driver, const char *buf,
151 size_t count);
155}; 152};
156 153
157#define DRIVER_ATTR(_name,_mode,_show,_store) \ 154#define DRIVER_ATTR(_name, _mode, _show, _store) \
158struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store) 155struct driver_attribute driver_attr_##_name = \
156 __ATTR(_name, _mode, _show, _store)
159 157
160extern int __must_check driver_create_file(struct device_driver *, 158extern int __must_check driver_create_file(struct device_driver *driver,
161 struct driver_attribute *); 159 struct driver_attribute *attr);
162extern void driver_remove_file(struct device_driver *, struct driver_attribute *); 160extern void driver_remove_file(struct device_driver *driver,
161 struct driver_attribute *attr);
163 162
164extern int __must_check driver_for_each_device(struct device_driver * drv, 163extern int __must_check driver_add_kobj(struct device_driver *drv,
165 struct device *start, void *data, 164 struct kobject *kobj,
166 int (*fn)(struct device *, void *)); 165 const char *fmt, ...);
167struct device * driver_find_device(struct device_driver *drv, 166
168 struct device *start, void *data, 167extern int __must_check driver_for_each_device(struct device_driver *drv,
169 int (*match)(struct device *, void *)); 168 struct device *start,
169 void *data,
170 int (*fn)(struct device *dev,
171 void *));
172struct device *driver_find_device(struct device_driver *drv,
173 struct device *start, void *data,
174 int (*match)(struct device *dev, void *data));
170 175
171/* 176/*
172 * device classes 177 * device classes
173 */ 178 */
174struct class { 179struct class {
175 const char * name; 180 const char *name;
176 struct module * owner; 181 struct module *owner;
177 182
178 struct kset subsys; 183 struct kset subsys;
179 struct list_head children; 184 struct list_head children;
180 struct list_head devices; 185 struct list_head devices;
181 struct list_head interfaces; 186 struct list_head interfaces;
182 struct kset class_dirs; 187 struct kset class_dirs;
183 struct semaphore sem; /* locks both the children and interfaces lists */ 188 struct semaphore sem; /* locks children, devices, interfaces */
184 189 struct class_attribute *class_attrs;
185 struct class_attribute * class_attrs; 190 struct class_device_attribute *class_dev_attrs;
186 struct class_device_attribute * class_dev_attrs; 191 struct device_attribute *dev_attrs;
187 struct device_attribute * dev_attrs;
188 192
189 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env); 193 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
190 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); 194 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
191 195
192 void (*release)(struct class_device *dev); 196 void (*release)(struct class_device *dev);
193 void (*class_release)(struct class *class); 197 void (*class_release)(struct class *class);
194 void (*dev_release)(struct device *dev); 198 void (*dev_release)(struct device *dev);
195 199
196 int (*suspend)(struct device *, pm_message_t state); 200 int (*suspend)(struct device *dev, pm_message_t state);
197 int (*resume)(struct device *); 201 int (*resume)(struct device *dev);
198}; 202};
199 203
200extern int __must_check class_register(struct class *); 204extern int __must_check class_register(struct class *class);
201extern void class_unregister(struct class *); 205extern void class_unregister(struct class *class);
206extern int class_for_each_device(struct class *class, void *data,
207 int (*fn)(struct device *dev, void *data));
208extern struct device *class_find_device(struct class *class, void *data,
209 int (*match)(struct device *, void *));
210extern struct class_device *class_find_child(struct class *class, void *data,
211 int (*match)(struct class_device *, void *));
202 212
203 213
204struct class_attribute { 214struct class_attribute {
205 struct attribute attr; 215 struct attribute attr;
206 ssize_t (*show)(struct class *, char * buf); 216 ssize_t (*show)(struct class *class, char *buf);
207 ssize_t (*store)(struct class *, const char * buf, size_t count); 217 ssize_t (*store)(struct class *class, const char *buf, size_t count);
208}; 218};
209 219
210#define CLASS_ATTR(_name,_mode,_show,_store) \ 220#define CLASS_ATTR(_name, _mode, _show, _store) \
211struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store) 221struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
212 222
213extern int __must_check class_create_file(struct class *, 223extern int __must_check class_create_file(struct class *class,
214 const struct class_attribute *); 224 const struct class_attribute *attr);
215extern void class_remove_file(struct class *, const struct class_attribute *); 225extern void class_remove_file(struct class *class,
226 const struct class_attribute *attr);
216 227
217struct class_device_attribute { 228struct class_device_attribute {
218 struct attribute attr; 229 struct attribute attr;
219 ssize_t (*show)(struct class_device *, char * buf); 230 ssize_t (*show)(struct class_device *, char *buf);
220 ssize_t (*store)(struct class_device *, const char * buf, size_t count); 231 ssize_t (*store)(struct class_device *, const char *buf, size_t count);
221}; 232};
222 233
223#define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \ 234#define CLASS_DEVICE_ATTR(_name, _mode, _show, _store) \
224struct class_device_attribute class_device_attr_##_name = \ 235struct class_device_attribute class_device_attr_##_name = \
225 __ATTR(_name,_mode,_show,_store) 236 __ATTR(_name, _mode, _show, _store)
226 237
227extern int __must_check class_device_create_file(struct class_device *, 238extern int __must_check class_device_create_file(struct class_device *,
228 const struct class_device_attribute *); 239 const struct class_device_attribute *);
@@ -255,26 +266,24 @@ struct class_device {
255 struct list_head node; 266 struct list_head node;
256 267
257 struct kobject kobj; 268 struct kobject kobj;
258 struct class * class; /* required */ 269 struct class *class;
259 dev_t devt; /* dev_t, creates the sysfs "dev" */ 270 dev_t devt;
260 struct device * dev; /* not necessary, but nice to have */ 271 struct device *dev;
261 void * class_data; /* class-specific data */ 272 void *class_data;
262 struct class_device *parent; /* parent of this child device, if there is one */ 273 struct class_device *parent;
263 struct attribute_group ** groups; /* optional groups */ 274 struct attribute_group **groups;
264 275
265 void (*release)(struct class_device *dev); 276 void (*release)(struct class_device *dev);
266 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env); 277 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
267 char class_id[BUS_ID_SIZE]; /* unique to this class */ 278 char class_id[BUS_ID_SIZE];
268}; 279};
269 280
270static inline void * 281static inline void *class_get_devdata(struct class_device *dev)
271class_get_devdata (struct class_device *dev)
272{ 282{
273 return dev->class_data; 283 return dev->class_data;
274} 284}
275 285
276static inline void 286static inline void class_set_devdata(struct class_device *dev, void *data)
277class_set_devdata (struct class_device *dev, void *data)
278{ 287{
279 dev->class_data = data; 288 dev->class_data = data;
280} 289}
@@ -286,10 +295,10 @@ extern void class_device_initialize(struct class_device *);
286extern int __must_check class_device_add(struct class_device *); 295extern int __must_check class_device_add(struct class_device *);
287extern void class_device_del(struct class_device *); 296extern void class_device_del(struct class_device *);
288 297
289extern struct class_device * class_device_get(struct class_device *); 298extern struct class_device *class_device_get(struct class_device *);
290extern void class_device_put(struct class_device *); 299extern void class_device_put(struct class_device *);
291 300
292extern void class_device_remove_file(struct class_device *, 301extern void class_device_remove_file(struct class_device *,
293 const struct class_device_attribute *); 302 const struct class_device_attribute *);
294extern int __must_check class_device_create_bin_file(struct class_device *, 303extern int __must_check class_device_create_bin_file(struct class_device *,
295 struct bin_attribute *); 304 struct bin_attribute *);
@@ -316,7 +325,7 @@ extern struct class_device *class_device_create(struct class *cls,
316 dev_t devt, 325 dev_t devt,
317 struct device *device, 326 struct device *device,
318 const char *fmt, ...) 327 const char *fmt, ...)
319 __attribute__((format(printf,5,6))); 328 __attribute__((format(printf, 5, 6)));
320extern void class_device_destroy(struct class *cls, dev_t devt); 329extern void class_device_destroy(struct class *cls, dev_t devt);
321 330
322/* 331/*
@@ -333,8 +342,8 @@ struct device_type {
333 struct attribute_group **groups; 342 struct attribute_group **groups;
334 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 343 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
335 void (*release)(struct device *dev); 344 void (*release)(struct device *dev);
336 int (*suspend)(struct device * dev, pm_message_t state); 345 int (*suspend)(struct device *dev, pm_message_t state);
337 int (*resume)(struct device * dev); 346 int (*resume)(struct device *dev);
338}; 347};
339 348
340/* interface for exporting device attributes */ 349/* interface for exporting device attributes */
@@ -346,18 +355,19 @@ struct device_attribute {
346 const char *buf, size_t count); 355 const char *buf, size_t count);
347}; 356};
348 357
349#define DEVICE_ATTR(_name,_mode,_show,_store) \ 358#define DEVICE_ATTR(_name, _mode, _show, _store) \
350struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store) 359struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
351 360
352extern int __must_check device_create_file(struct device *device, 361extern int __must_check device_create_file(struct device *device,
353 struct device_attribute * entry); 362 struct device_attribute *entry);
354extern void device_remove_file(struct device * dev, struct device_attribute * attr); 363extern void device_remove_file(struct device *dev,
364 struct device_attribute *attr);
355extern int __must_check device_create_bin_file(struct device *dev, 365extern int __must_check device_create_bin_file(struct device *dev,
356 struct bin_attribute *attr); 366 struct bin_attribute *attr);
357extern void device_remove_bin_file(struct device *dev, 367extern void device_remove_bin_file(struct device *dev,
358 struct bin_attribute *attr); 368 struct bin_attribute *attr);
359extern int device_schedule_callback_owner(struct device *dev, 369extern int device_schedule_callback_owner(struct device *dev,
360 void (*func)(struct device *), struct module *owner); 370 void (*func)(struct device *dev), struct module *owner);
361 371
362/* This is a macro to avoid include problems with THIS_MODULE */ 372/* This is a macro to avoid include problems with THIS_MODULE */
363#define device_schedule_callback(dev, func) \ 373#define device_schedule_callback(dev, func) \
@@ -368,21 +378,21 @@ typedef void (*dr_release_t)(struct device *dev, void *res);
368typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data); 378typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
369 379
370#ifdef CONFIG_DEBUG_DEVRES 380#ifdef CONFIG_DEBUG_DEVRES
371extern void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp, 381extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
372 const char *name); 382 const char *name);
373#define devres_alloc(release, size, gfp) \ 383#define devres_alloc(release, size, gfp) \
374 __devres_alloc(release, size, gfp, #release) 384 __devres_alloc(release, size, gfp, #release)
375#else 385#else
376extern void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp); 386extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
377#endif 387#endif
378extern void devres_free(void *res); 388extern void devres_free(void *res);
379extern void devres_add(struct device *dev, void *res); 389extern void devres_add(struct device *dev, void *res);
380extern void * devres_find(struct device *dev, dr_release_t release, 390extern void *devres_find(struct device *dev, dr_release_t release,
381 dr_match_t match, void *match_data);
382extern void * devres_get(struct device *dev, void *new_res,
383 dr_match_t match, void *match_data); 391 dr_match_t match, void *match_data);
384extern void * devres_remove(struct device *dev, dr_release_t release, 392extern void *devres_get(struct device *dev, void *new_res,
385 dr_match_t match, void *match_data); 393 dr_match_t match, void *match_data);
394extern void *devres_remove(struct device *dev, dr_release_t release,
395 dr_match_t match, void *match_data);
386extern int devres_destroy(struct device *dev, dr_release_t release, 396extern int devres_destroy(struct device *dev, dr_release_t release,
387 dr_match_t match, void *match_data); 397 dr_match_t match, void *match_data);
388 398
@@ -399,7 +409,7 @@ extern void devm_kfree(struct device *dev, void *p);
399 409
400struct device { 410struct device {
401 struct klist klist_children; 411 struct klist klist_children;
402 struct klist_node knode_parent; /* node in sibling list */ 412 struct klist_node knode_parent; /* node in sibling list */
403 struct klist_node knode_driver; 413 struct klist_node knode_driver;
404 struct klist_node knode_bus; 414 struct klist_node knode_bus;
405 struct device *parent; 415 struct device *parent;
@@ -414,7 +424,7 @@ struct device {
414 * its driver. 424 * its driver.
415 */ 425 */
416 426
417 struct bus_type * bus; /* type of bus device is on */ 427 struct bus_type *bus; /* type of bus device is on */
418 struct device_driver *driver; /* which driver has allocated this 428 struct device_driver *driver; /* which driver has allocated this
419 device */ 429 device */
420 void *driver_data; /* data private to the driver */ 430 void *driver_data; /* data private to the driver */
@@ -445,10 +455,10 @@ struct device {
445 /* class_device migration path */ 455 /* class_device migration path */
446 struct list_head node; 456 struct list_head node;
447 struct class *class; 457 struct class *class;
448 dev_t devt; /* dev_t, creates the sysfs "dev" */ 458 dev_t devt; /* dev_t, creates the sysfs "dev" */
449 struct attribute_group **groups; /* optional groups */ 459 struct attribute_group **groups; /* optional groups */
450 460
451 void (*release)(struct device * dev); 461 void (*release)(struct device *dev);
452}; 462};
453 463
454#ifdef CONFIG_NUMA 464#ifdef CONFIG_NUMA
@@ -470,14 +480,12 @@ static inline void set_dev_node(struct device *dev, int node)
470} 480}
471#endif 481#endif
472 482
473static inline void * 483static inline void *dev_get_drvdata(struct device *dev)
474dev_get_drvdata (struct device *dev)
475{ 484{
476 return dev->driver_data; 485 return dev->driver_data;
477} 486}
478 487
479static inline void 488static inline void dev_set_drvdata(struct device *dev, void *data)
480dev_set_drvdata (struct device *dev, void *data)
481{ 489{
482 dev->driver_data = data; 490 dev->driver_data = data;
483} 491}
@@ -492,15 +500,15 @@ void driver_init(void);
492/* 500/*
493 * High level routines for use by the bus drivers 501 * High level routines for use by the bus drivers
494 */ 502 */
495extern int __must_check device_register(struct device * dev); 503extern int __must_check device_register(struct device *dev);
496extern void device_unregister(struct device * dev); 504extern void device_unregister(struct device *dev);
497extern void device_initialize(struct device * dev); 505extern void device_initialize(struct device *dev);
498extern int __must_check device_add(struct device * dev); 506extern int __must_check device_add(struct device *dev);
499extern void device_del(struct device * dev); 507extern void device_del(struct device *dev);
500extern int device_for_each_child(struct device *, void *, 508extern int device_for_each_child(struct device *dev, void *data,
501 int (*fn)(struct device *, void *)); 509 int (*fn)(struct device *dev, void *data));
502extern struct device *device_find_child(struct device *, void *data, 510extern struct device *device_find_child(struct device *dev, void *data,
503 int (*match)(struct device *, void *)); 511 int (*match)(struct device *dev, void *data));
504extern int device_rename(struct device *dev, char *new_name); 512extern int device_rename(struct device *dev, char *new_name);
505extern int device_move(struct device *dev, struct device *new_parent); 513extern int device_move(struct device *dev, struct device *new_parent);
506 514
@@ -509,8 +517,8 @@ extern int device_move(struct device *dev, struct device *new_parent);
509 * for information on use. 517 * for information on use.
510 */ 518 */
511extern int __must_check device_bind_driver(struct device *dev); 519extern int __must_check device_bind_driver(struct device *dev);
512extern void device_release_driver(struct device * dev); 520extern void device_release_driver(struct device *dev);
513extern int __must_check device_attach(struct device * dev); 521extern int __must_check device_attach(struct device *dev);
514extern int __must_check driver_attach(struct device_driver *drv); 522extern int __must_check driver_attach(struct device_driver *drv);
515extern int __must_check device_reprobe(struct device *dev); 523extern int __must_check device_reprobe(struct device *dev);
516 524
@@ -519,8 +527,16 @@ extern int __must_check device_reprobe(struct device *dev);
519 */ 527 */
520extern struct device *device_create(struct class *cls, struct device *parent, 528extern struct device *device_create(struct class *cls, struct device *parent,
521 dev_t devt, const char *fmt, ...) 529 dev_t devt, const char *fmt, ...)
522 __attribute__((format(printf,4,5))); 530 __attribute__((format(printf, 4, 5)));
523extern void device_destroy(struct class *cls, dev_t devt); 531extern void device_destroy(struct class *cls, dev_t devt);
532#ifdef CONFIG_PM_SLEEP
533extern void destroy_suspended_device(struct class *cls, dev_t devt);
534#else /* !CONFIG_PM_SLEEP */
535static inline void destroy_suspended_device(struct class *cls, dev_t devt)
536{
537 device_destroy(cls, devt);
538}
539#endif /* !CONFIG_PM_SLEEP */
524 540
525/* 541/*
526 * Platform "fixup" functions - allow the platform to have their say 542 * Platform "fixup" functions - allow the platform to have their say
@@ -528,17 +544,17 @@ extern void device_destroy(struct class *cls, dev_t devt);
528 * know about. 544 * know about.
529 */ 545 */
530/* Notify platform of device discovery */ 546/* Notify platform of device discovery */
531extern int (*platform_notify)(struct device * dev); 547extern int (*platform_notify)(struct device *dev);
532 548
533extern int (*platform_notify_remove)(struct device * dev); 549extern int (*platform_notify_remove)(struct device *dev);
534 550
535 551
536/** 552/**
537 * get_device - atomically increment the reference count for the device. 553 * get_device - atomically increment the reference count for the device.
538 * 554 *
539 */ 555 */
540extern struct device * get_device(struct device * dev); 556extern struct device *get_device(struct device *dev);
541extern void put_device(struct device * dev); 557extern void put_device(struct device *dev);
542 558
543 559
544/* drivers/base/power/shutdown.c */ 560/* drivers/base/power/shutdown.c */
@@ -547,22 +563,33 @@ extern void device_shutdown(void);
547/* drivers/base/sys.c */ 563/* drivers/base/sys.c */
548extern void sysdev_shutdown(void); 564extern void sysdev_shutdown(void);
549 565
550
551/* drivers/base/firmware.c */
552extern int __must_check firmware_register(struct kset *);
553extern void firmware_unregister(struct kset *);
554
555/* debugging and troubleshooting/diagnostic helpers. */ 566/* debugging and troubleshooting/diagnostic helpers. */
556extern const char *dev_driver_string(struct device *dev); 567extern const char *dev_driver_string(struct device *dev);
557#define dev_printk(level, dev, format, arg...) \ 568#define dev_printk(level, dev, format, arg...) \
558 printk(level "%s %s: " format , dev_driver_string(dev) , (dev)->bus_id , ## arg) 569 printk(level "%s %s: " format , dev_driver_string(dev) , \
570 (dev)->bus_id , ## arg)
571
572#define dev_emerg(dev, format, arg...) \
573 dev_printk(KERN_EMERG , dev , format , ## arg)
574#define dev_alert(dev, format, arg...) \
575 dev_printk(KERN_ALERT , dev , format , ## arg)
576#define dev_crit(dev, format, arg...) \
577 dev_printk(KERN_CRIT , dev , format , ## arg)
578#define dev_err(dev, format, arg...) \
579 dev_printk(KERN_ERR , dev , format , ## arg)
580#define dev_warn(dev, format, arg...) \
581 dev_printk(KERN_WARNING , dev , format , ## arg)
582#define dev_notice(dev, format, arg...) \
583 dev_printk(KERN_NOTICE , dev , format , ## arg)
584#define dev_info(dev, format, arg...) \
585 dev_printk(KERN_INFO , dev , format , ## arg)
559 586
560#ifdef DEBUG 587#ifdef DEBUG
561#define dev_dbg(dev, format, arg...) \ 588#define dev_dbg(dev, format, arg...) \
562 dev_printk(KERN_DEBUG , dev , format , ## arg) 589 dev_printk(KERN_DEBUG , dev , format , ## arg)
563#else 590#else
564static inline int __attribute__ ((format (printf, 2, 3))) 591static inline int __attribute__ ((format (printf, 2, 3)))
565dev_dbg(struct device * dev, const char * fmt, ...) 592dev_dbg(struct device *dev, const char *fmt, ...)
566{ 593{
567 return 0; 594 return 0;
568} 595}
@@ -572,21 +599,12 @@ dev_dbg(struct device * dev, const char * fmt, ...)
572#define dev_vdbg dev_dbg 599#define dev_vdbg dev_dbg
573#else 600#else
574static inline int __attribute__ ((format (printf, 2, 3))) 601static inline int __attribute__ ((format (printf, 2, 3)))
575dev_vdbg(struct device * dev, const char * fmt, ...) 602dev_vdbg(struct device *dev, const char *fmt, ...)
576{ 603{
577 return 0; 604 return 0;
578} 605}
579#endif 606#endif
580 607
581#define dev_err(dev, format, arg...) \
582 dev_printk(KERN_ERR , dev , format , ## arg)
583#define dev_info(dev, format, arg...) \
584 dev_printk(KERN_INFO , dev , format , ## arg)
585#define dev_warn(dev, format, arg...) \
586 dev_printk(KERN_WARNING , dev , format , ## arg)
587#define dev_notice(dev, format, arg...) \
588 dev_printk(KERN_NOTICE , dev , format , ## arg)
589
590/* Create alias, so I can be autoloaded. */ 608/* Create alias, so I can be autoloaded. */
591#define MODULE_ALIAS_CHARDEV(major,minor) \ 609#define MODULE_ALIAS_CHARDEV(major,minor) \
592 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor)) 610 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
index a3b6035b6c86..55c9a6952f44 100644
--- a/include/linux/dmaengine.h
+++ b/include/linux/dmaengine.h
@@ -132,7 +132,7 @@ struct dma_chan {
132 132
133 /* sysfs */ 133 /* sysfs */
134 int chan_id; 134 int chan_id;
135 struct class_device class_dev; 135 struct device dev;
136 136
137 struct kref refcount; 137 struct kref refcount;
138 int slow_ref; 138 int slow_ref;
@@ -142,6 +142,7 @@ struct dma_chan {
142 struct dma_chan_percpu *local; 142 struct dma_chan_percpu *local;
143}; 143};
144 144
145#define to_dma_chan(p) container_of(p, struct dma_chan, dev)
145 146
146void dma_chan_cleanup(struct kref *kref); 147void dma_chan_cleanup(struct kref *kref);
147 148
diff --git a/include/linux/fs.h b/include/linux/fs.h
index b3ec4a496d64..21398a5d688d 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1476,7 +1476,7 @@ extern void drop_collected_mounts(struct vfsmount *);
1476extern int vfs_statfs(struct dentry *, struct kstatfs *); 1476extern int vfs_statfs(struct dentry *, struct kstatfs *);
1477 1477
1478/* /sys/fs */ 1478/* /sys/fs */
1479extern struct kset fs_subsys; 1479extern struct kobject *fs_kobj;
1480 1480
1481#define FLOCK_VERIFY_READ 1 1481#define FLOCK_VERIFY_READ 1
1482#define FLOCK_VERIFY_WRITE 2 1482#define FLOCK_VERIFY_WRITE 2
diff --git a/include/linux/genhd.h b/include/linux/genhd.h
index a47b8025d399..1dbea0ac5693 100644
--- a/include/linux/genhd.h
+++ b/include/linux/genhd.h
@@ -10,9 +10,19 @@
10 */ 10 */
11 11
12#include <linux/types.h> 12#include <linux/types.h>
13#include <linux/kdev_t.h>
13 14
14#ifdef CONFIG_BLOCK 15#ifdef CONFIG_BLOCK
15 16
17#define kobj_to_dev(k) container_of(k, struct device, kobj)
18#define dev_to_disk(device) container_of(device, struct gendisk, dev)
19#define dev_to_part(device) container_of(device, struct hd_struct, dev)
20
21extern struct device_type disk_type;
22extern struct device_type part_type;
23extern struct kobject *block_depr;
24extern struct class block_class;
25
16enum { 26enum {
17/* These three have identical behaviour; use the second one if DOS FDISK gets 27/* These three have identical behaviour; use the second one if DOS FDISK gets
18 confused about extended/logical partitions starting past cylinder 1023. */ 28 confused about extended/logical partitions starting past cylinder 1023. */
@@ -84,7 +94,7 @@ struct partition {
84struct hd_struct { 94struct hd_struct {
85 sector_t start_sect; 95 sector_t start_sect;
86 sector_t nr_sects; 96 sector_t nr_sects;
87 struct kobject kobj; 97 struct device dev;
88 struct kobject *holder_dir; 98 struct kobject *holder_dir;
89 unsigned ios[2], sectors[2]; /* READs and WRITEs */ 99 unsigned ios[2], sectors[2]; /* READs and WRITEs */
90 int policy, partno; 100 int policy, partno;
@@ -117,15 +127,14 @@ struct gendisk {
117 * disks that can't be partitioned. */ 127 * disks that can't be partitioned. */
118 char disk_name[32]; /* name of major driver */ 128 char disk_name[32]; /* name of major driver */
119 struct hd_struct **part; /* [indexed by minor] */ 129 struct hd_struct **part; /* [indexed by minor] */
120 int part_uevent_suppress;
121 struct block_device_operations *fops; 130 struct block_device_operations *fops;
122 struct request_queue *queue; 131 struct request_queue *queue;
123 void *private_data; 132 void *private_data;
124 sector_t capacity; 133 sector_t capacity;
125 134
126 int flags; 135 int flags;
127 struct device *driverfs_dev; 136 struct device *driverfs_dev; // FIXME: remove
128 struct kobject kobj; 137 struct device dev;
129 struct kobject *holder_dir; 138 struct kobject *holder_dir;
130 struct kobject *slave_dir; 139 struct kobject *slave_dir;
131 140
@@ -143,13 +152,6 @@ struct gendisk {
143 struct work_struct async_notify; 152 struct work_struct async_notify;
144}; 153};
145 154
146/* Structure for sysfs attributes on block devices */
147struct disk_attribute {
148 struct attribute attr;
149 ssize_t (*show)(struct gendisk *, char *);
150 ssize_t (*store)(struct gendisk *, const char *, size_t);
151};
152
153/* 155/*
154 * Macros to operate on percpu disk statistics: 156 * Macros to operate on percpu disk statistics:
155 * 157 *
@@ -411,7 +413,8 @@ struct unixware_disklabel {
411#define ADDPART_FLAG_RAID 1 413#define ADDPART_FLAG_RAID 1
412#define ADDPART_FLAG_WHOLEDISK 2 414#define ADDPART_FLAG_WHOLEDISK 2
413 415
414char *disk_name (struct gendisk *hd, int part, char *buf); 416extern dev_t blk_lookup_devt(const char *name);
417extern char *disk_name (struct gendisk *hd, int part, char *buf);
415 418
416extern int rescan_partitions(struct gendisk *disk, struct block_device *bdev); 419extern int rescan_partitions(struct gendisk *disk, struct block_device *bdev);
417extern void add_partition(struct gendisk *, int, sector_t, sector_t, int); 420extern void add_partition(struct gendisk *, int, sector_t, sector_t, int);
@@ -423,12 +426,12 @@ extern struct gendisk *alloc_disk(int minors);
423extern struct kobject *get_disk(struct gendisk *disk); 426extern struct kobject *get_disk(struct gendisk *disk);
424extern void put_disk(struct gendisk *disk); 427extern void put_disk(struct gendisk *disk);
425extern void genhd_media_change_notify(struct gendisk *disk); 428extern void genhd_media_change_notify(struct gendisk *disk);
426extern void blk_register_region(dev_t dev, unsigned long range, 429extern void blk_register_region(dev_t devt, unsigned long range,
427 struct module *module, 430 struct module *module,
428 struct kobject *(*probe)(dev_t, int *, void *), 431 struct kobject *(*probe)(dev_t, int *, void *),
429 int (*lock)(dev_t, void *), 432 int (*lock)(dev_t, void *),
430 void *data); 433 void *data);
431extern void blk_unregister_region(dev_t dev, unsigned long range); 434extern void blk_unregister_region(dev_t devt, unsigned long range);
432 435
433static inline struct block_device *bdget_disk(struct gendisk *disk, int index) 436static inline struct block_device *bdget_disk(struct gendisk *disk, int index)
434{ 437{
@@ -441,6 +444,12 @@ static inline struct block_device *bdget_disk(struct gendisk *disk, int index)
441 444
442static inline void printk_all_partitions(void) { } 445static inline void printk_all_partitions(void) { }
443 446
447static inline dev_t blk_lookup_devt(const char *name)
448{
449 dev_t devt = MKDEV(0, 0);
450 return devt;
451}
452
444#endif /* CONFIG_BLOCK */ 453#endif /* CONFIG_BLOCK */
445 454
446#endif 455#endif
diff --git a/include/linux/kobject.h b/include/linux/kobject.h
index 4a0d27f475d7..caa3f411f15d 100644
--- a/include/linux/kobject.h
+++ b/include/linux/kobject.h
@@ -3,15 +3,14 @@
3 * 3 *
4 * Copyright (c) 2002-2003 Patrick Mochel 4 * Copyright (c) 2002-2003 Patrick Mochel
5 * Copyright (c) 2002-2003 Open Source Development Labs 5 * Copyright (c) 2002-2003 Open Source Development Labs
6 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com> 6 * Copyright (c) 2006-2008 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (c) 2006-2007 Novell Inc. 7 * Copyright (c) 2006-2008 Novell Inc.
8 * 8 *
9 * This file is released under the GPLv2. 9 * This file is released under the GPLv2.
10 * 10 *
11 *
12 * Please read Documentation/kobject.txt before using the kobject 11 * Please read Documentation/kobject.txt before using the kobject
13 * interface, ESPECIALLY the parts about reference counts and object 12 * interface, ESPECIALLY the parts about reference counts and object
14 * destructors. 13 * destructors.
15 */ 14 */
16 15
17#ifndef _KOBJECT_H_ 16#ifndef _KOBJECT_H_
@@ -61,48 +60,54 @@ enum kobject_action {
61}; 60};
62 61
63struct kobject { 62struct kobject {
64 const char * k_name; 63 const char *name;
65 struct kref kref; 64 struct kref kref;
66 struct list_head entry; 65 struct list_head entry;
67 struct kobject * parent; 66 struct kobject *parent;
68 struct kset * kset; 67 struct kset *kset;
69 struct kobj_type * ktype; 68 struct kobj_type *ktype;
70 struct sysfs_dirent * sd; 69 struct sysfs_dirent *sd;
70 unsigned int state_initialized:1;
71 unsigned int state_in_sysfs:1;
72 unsigned int state_add_uevent_sent:1;
73 unsigned int state_remove_uevent_sent:1;
71}; 74};
72 75
73extern int kobject_set_name(struct kobject *, const char *, ...) 76extern int kobject_set_name(struct kobject *kobj, const char *name, ...)
74 __attribute__((format(printf,2,3))); 77 __attribute__((format(printf, 2, 3)));
75 78
76static inline const char * kobject_name(const struct kobject * kobj) 79static inline const char *kobject_name(const struct kobject *kobj)
77{ 80{
78 return kobj->k_name; 81 return kobj->name;
79} 82}
80 83
81extern void kobject_init(struct kobject *); 84extern void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
82extern void kobject_cleanup(struct kobject *); 85extern int __must_check kobject_add(struct kobject *kobj,
86 struct kobject *parent,
87 const char *fmt, ...);
88extern int __must_check kobject_init_and_add(struct kobject *kobj,
89 struct kobj_type *ktype,
90 struct kobject *parent,
91 const char *fmt, ...);
92
93extern void kobject_del(struct kobject *kobj);
83 94
84extern int __must_check kobject_add(struct kobject *); 95extern struct kobject * __must_check kobject_create(void);
85extern void kobject_del(struct kobject *); 96extern struct kobject * __must_check kobject_create_and_add(const char *name,
97 struct kobject *parent);
86 98
87extern int __must_check kobject_rename(struct kobject *, const char *new_name); 99extern int __must_check kobject_rename(struct kobject *, const char *new_name);
88extern int __must_check kobject_move(struct kobject *, struct kobject *); 100extern int __must_check kobject_move(struct kobject *, struct kobject *);
89 101
90extern int __must_check kobject_register(struct kobject *); 102extern struct kobject *kobject_get(struct kobject *kobj);
91extern void kobject_unregister(struct kobject *); 103extern void kobject_put(struct kobject *kobj);
92
93extern struct kobject * kobject_get(struct kobject *);
94extern void kobject_put(struct kobject *);
95
96extern struct kobject *kobject_kset_add_dir(struct kset *kset,
97 struct kobject *, const char *);
98extern struct kobject *kobject_add_dir(struct kobject *, const char *);
99 104
100extern char * kobject_get_path(struct kobject *, gfp_t); 105extern char *kobject_get_path(struct kobject *kobj, gfp_t flag);
101 106
102struct kobj_type { 107struct kobj_type {
103 void (*release)(struct kobject *); 108 void (*release)(struct kobject *kobj);
104 struct sysfs_ops * sysfs_ops; 109 struct sysfs_ops *sysfs_ops;
105 struct attribute ** default_attrs; 110 struct attribute **default_attrs;
106}; 111};
107 112
108struct kobj_uevent_env { 113struct kobj_uevent_env {
@@ -119,6 +124,16 @@ struct kset_uevent_ops {
119 struct kobj_uevent_env *env); 124 struct kobj_uevent_env *env);
120}; 125};
121 126
127struct kobj_attribute {
128 struct attribute attr;
129 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
130 char *buf);
131 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
132 const char *buf, size_t count);
133};
134
135extern struct sysfs_ops kobj_sysfs_ops;
136
122/** 137/**
123 * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem. 138 * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
124 * 139 *
@@ -128,7 +143,6 @@ struct kset_uevent_ops {
128 * define the attribute callbacks and other common events that happen to 143 * define the attribute callbacks and other common events that happen to
129 * a kobject. 144 * a kobject.
130 * 145 *
131 * @ktype: the struct kobj_type for this specific kset
132 * @list: the list of all kobjects for this kset 146 * @list: the list of all kobjects for this kset
133 * @list_lock: a lock for iterating over the kobjects 147 * @list_lock: a lock for iterating over the kobjects
134 * @kobj: the embedded kobject for this kset (recursion, isn't it fun...) 148 * @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
@@ -138,99 +152,49 @@ struct kset_uevent_ops {
138 * desired. 152 * desired.
139 */ 153 */
140struct kset { 154struct kset {
141 struct kobj_type *ktype; 155 struct list_head list;
142 struct list_head list; 156 spinlock_t list_lock;
143 spinlock_t list_lock; 157 struct kobject kobj;
144 struct kobject kobj; 158 struct kset_uevent_ops *uevent_ops;
145 struct kset_uevent_ops *uevent_ops;
146}; 159};
147 160
161extern void kset_init(struct kset *kset);
162extern int __must_check kset_register(struct kset *kset);
163extern void kset_unregister(struct kset *kset);
164extern struct kset * __must_check kset_create_and_add(const char *name,
165 struct kset_uevent_ops *u,
166 struct kobject *parent_kobj);
148 167
149extern void kset_init(struct kset * k); 168static inline struct kset *to_kset(struct kobject *kobj)
150extern int __must_check kset_add(struct kset * k);
151extern int __must_check kset_register(struct kset * k);
152extern void kset_unregister(struct kset * k);
153
154static inline struct kset * to_kset(struct kobject * kobj)
155{ 169{
156 return kobj ? container_of(kobj,struct kset,kobj) : NULL; 170 return kobj ? container_of(kobj, struct kset, kobj) : NULL;
157} 171}
158 172
159static inline struct kset * kset_get(struct kset * k) 173static inline struct kset *kset_get(struct kset *k)
160{ 174{
161 return k ? to_kset(kobject_get(&k->kobj)) : NULL; 175 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
162} 176}
163 177
164static inline void kset_put(struct kset * k) 178static inline void kset_put(struct kset *k)
165{ 179{
166 kobject_put(&k->kobj); 180 kobject_put(&k->kobj);
167} 181}
168 182
169static inline struct kobj_type * get_ktype(struct kobject * k) 183static inline struct kobj_type *get_ktype(struct kobject *kobj)
170{ 184{
171 if (k->kset && k->kset->ktype) 185 return kobj->ktype;
172 return k->kset->ktype;
173 else
174 return k->ktype;
175} 186}
176 187
177extern struct kobject * kset_find_obj(struct kset *, const char *); 188extern struct kobject *kset_find_obj(struct kset *, const char *);
178
179
180/*
181 * Use this when initializing an embedded kset with no other
182 * fields to initialize.
183 */
184#define set_kset_name(str) .kset = { .kobj = { .k_name = str } }
185
186
187#define decl_subsys(_name,_type,_uevent_ops) \
188struct kset _name##_subsys = { \
189 .kobj = { .k_name = __stringify(_name) }, \
190 .ktype = _type, \
191 .uevent_ops =_uevent_ops, \
192}
193#define decl_subsys_name(_varname,_name,_type,_uevent_ops) \
194struct kset _varname##_subsys = { \
195 .kobj = { .k_name = __stringify(_name) }, \
196 .ktype = _type, \
197 .uevent_ops =_uevent_ops, \
198}
199
200/* The global /sys/kernel/ subsystem for people to chain off of */
201extern struct kset kernel_subsys;
202/* The global /sys/hypervisor/ subsystem */
203extern struct kset hypervisor_subsys;
204
205/*
206 * Helpers for setting the kset of registered objects.
207 * Often, a registered object belongs to a kset embedded in a
208 * subsystem. These do no magic, just make the resulting code
209 * easier to follow.
210 */
211
212/**
213 * kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
214 * @obj: ptr to some object type.
215 * @subsys: a subsystem object (not a ptr).
216 *
217 * Can be used for any object type with an embedded ->kobj.
218 */
219
220#define kobj_set_kset_s(obj,subsys) \
221 (obj)->kobj.kset = &(subsys)
222
223extern int __must_check subsystem_register(struct kset *);
224extern void subsystem_unregister(struct kset *);
225
226struct subsys_attribute {
227 struct attribute attr;
228 ssize_t (*show)(struct kset *, char *);
229 ssize_t (*store)(struct kset *, const char *, size_t);
230};
231 189
232extern int __must_check subsys_create_file(struct kset *, 190/* The global /sys/kernel/ kobject for people to chain off of */
233 struct subsys_attribute *); 191extern struct kobject *kernel_kobj;
192/* The global /sys/hypervisor/ kobject for people to chain off of */
193extern struct kobject *hypervisor_kobj;
194/* The global /sys/power/ kobject for people to chain off of */
195extern struct kobject *power_kobj;
196/* The global /sys/firmware/ kobject for people to chain off of */
197extern struct kobject *firmware_kobj;
234 198
235#if defined(CONFIG_HOTPLUG) 199#if defined(CONFIG_HOTPLUG)
236int kobject_uevent(struct kobject *kobj, enum kobject_action action); 200int kobject_uevent(struct kobject *kobj, enum kobject_action action);
@@ -243,18 +207,20 @@ int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
243int kobject_action_type(const char *buf, size_t count, 207int kobject_action_type(const char *buf, size_t count,
244 enum kobject_action *type); 208 enum kobject_action *type);
245#else 209#else
246static inline int kobject_uevent(struct kobject *kobj, enum kobject_action action) 210static inline int kobject_uevent(struct kobject *kobj,
211 enum kobject_action action)
247{ return 0; } 212{ return 0; }
248static inline int kobject_uevent_env(struct kobject *kobj, 213static inline int kobject_uevent_env(struct kobject *kobj,
249 enum kobject_action action, 214 enum kobject_action action,
250 char *envp[]) 215 char *envp[])
251{ return 0; } 216{ return 0; }
252 217
253static inline int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...) 218static inline int add_uevent_var(struct kobj_uevent_env *env,
219 const char *format, ...)
254{ return 0; } 220{ return 0; }
255 221
256static inline int kobject_action_type(const char *buf, size_t count, 222static inline int kobject_action_type(const char *buf, size_t count,
257 enum kobject_action *type) 223 enum kobject_action *type)
258{ return -EINVAL; } 224{ return -EINVAL; }
259#endif 225#endif
260 226
diff --git a/include/linux/kref.h b/include/linux/kref.h
index 6fee3539893f..5d185635786e 100644
--- a/include/linux/kref.h
+++ b/include/linux/kref.h
@@ -24,6 +24,7 @@ struct kref {
24 atomic_t refcount; 24 atomic_t refcount;
25}; 25};
26 26
27void kref_set(struct kref *kref, int num);
27void kref_init(struct kref *kref); 28void kref_init(struct kref *kref);
28void kref_get(struct kref *kref); 29void kref_get(struct kref *kref);
29int kref_put(struct kref *kref, void (*release) (struct kref *kref)); 30int kref_put(struct kref *kref, void (*release) (struct kref *kref));
diff --git a/include/linux/module.h b/include/linux/module.h
index 2cbc0b87e329..c97bdb7eb957 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -574,7 +574,9 @@ struct device_driver;
574#ifdef CONFIG_SYSFS 574#ifdef CONFIG_SYSFS
575struct module; 575struct module;
576 576
577extern struct kset module_subsys; 577extern struct kset *module_kset;
578extern struct kobj_type module_ktype;
579extern int module_sysfs_initialized;
578 580
579int mod_sysfs_init(struct module *mod); 581int mod_sysfs_init(struct module *mod);
580int mod_sysfs_setup(struct module *mod, 582int mod_sysfs_setup(struct module *mod,
@@ -607,21 +609,6 @@ static inline void module_remove_modinfo_attrs(struct module *mod)
607 609
608#endif /* CONFIG_SYSFS */ 610#endif /* CONFIG_SYSFS */
609 611
610#if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
611
612void module_add_driver(struct module *mod, struct device_driver *drv);
613void module_remove_driver(struct device_driver *drv);
614
615#else /* not both CONFIG_SYSFS && CONFIG_MODULES */
616
617static inline void module_add_driver(struct module *mod, struct device_driver *drv)
618{ }
619
620static inline void module_remove_driver(struct device_driver *drv)
621{ }
622
623#endif
624
625#define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x) 612#define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
626 613
627/* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */ 614/* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
diff --git a/include/linux/pci_hotplug.h b/include/linux/pci_hotplug.h
index ab4cb6ecd47c..8f67e8f2a3cc 100644
--- a/include/linux/pci_hotplug.h
+++ b/include/linux/pci_hotplug.h
@@ -174,7 +174,7 @@ extern int pci_hp_register (struct hotplug_slot *slot);
174extern int pci_hp_deregister (struct hotplug_slot *slot); 174extern int pci_hp_deregister (struct hotplug_slot *slot);
175extern int __must_check pci_hp_change_slot_info (struct hotplug_slot *slot, 175extern int __must_check pci_hp_change_slot_info (struct hotplug_slot *slot,
176 struct hotplug_slot_info *info); 176 struct hotplug_slot_info *info);
177extern struct kset pci_hotplug_slots_subsys; 177extern struct kset *pci_hotplug_slots_kset;
178 178
179/* PCI Setting Record (Type 0) */ 179/* PCI Setting Record (Type 0) */
180struct hpp_type0 { 180struct hpp_type0 {
diff --git a/include/linux/pktcdvd.h b/include/linux/pktcdvd.h
index 5ea4f05683f6..04b4d7330e6d 100644
--- a/include/linux/pktcdvd.h
+++ b/include/linux/pktcdvd.h
@@ -290,7 +290,7 @@ struct pktcdvd_device
290 int write_congestion_off; 290 int write_congestion_off;
291 int write_congestion_on; 291 int write_congestion_on;
292 292
293 struct class_device *clsdev; /* sysfs pktcdvd[0-7] class dev */ 293 struct device *dev; /* sysfs pktcdvd[0-7] dev */
294 struct pktcdvd_kobj *kobj_stat; /* sysfs pktcdvd[0-7]/stat/ */ 294 struct pktcdvd_kobj *kobj_stat; /* sysfs pktcdvd[0-7]/stat/ */
295 struct pktcdvd_kobj *kobj_wqueue; /* sysfs pktcdvd[0-7]/write_queue/ */ 295 struct pktcdvd_kobj *kobj_wqueue; /* sysfs pktcdvd[0-7]/write_queue/ */
296 296
diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
index e80804316cdb..3261681c82a4 100644
--- a/include/linux/platform_device.h
+++ b/include/linux/platform_device.h
@@ -35,7 +35,7 @@ extern struct resource *platform_get_resource_byname(struct platform_device *, u
35extern int platform_get_irq_byname(struct platform_device *, char *); 35extern int platform_get_irq_byname(struct platform_device *, char *);
36extern int platform_add_devices(struct platform_device **, int); 36extern int platform_add_devices(struct platform_device **, int);
37 37
38extern struct platform_device *platform_device_register_simple(char *, int id, 38extern struct platform_device *platform_device_register_simple(const char *, int id,
39 struct resource *, unsigned int); 39 struct resource *, unsigned int);
40 40
41extern struct platform_device *platform_device_alloc(const char *name, int id); 41extern struct platform_device *platform_device_alloc(const char *name, int id);
diff --git a/include/linux/sched.h b/include/linux/sched.h
index cc14656f8682..d6eacda765ca 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -552,18 +552,13 @@ struct user_struct {
552#ifdef CONFIG_FAIR_USER_SCHED 552#ifdef CONFIG_FAIR_USER_SCHED
553 struct task_group *tg; 553 struct task_group *tg;
554#ifdef CONFIG_SYSFS 554#ifdef CONFIG_SYSFS
555 struct kset kset; 555 struct kobject kobj;
556 struct subsys_attribute user_attr;
557 struct work_struct work; 556 struct work_struct work;
558#endif 557#endif
559#endif 558#endif
560}; 559};
561 560
562#ifdef CONFIG_FAIR_USER_SCHED 561extern int uids_sysfs_init(void);
563extern int uids_kobject_init(void);
564#else
565static inline int uids_kobject_init(void) { return 0; }
566#endif
567 562
568extern struct user_struct *find_user(uid_t); 563extern struct user_struct *find_user(uid_t);
569 564
diff --git a/include/linux/sysdev.h b/include/linux/sysdev.h
index e285746588d6..f752e73bf977 100644
--- a/include/linux/sysdev.h
+++ b/include/linux/sysdev.h
@@ -29,6 +29,7 @@
29struct sys_device; 29struct sys_device;
30 30
31struct sysdev_class { 31struct sysdev_class {
32 const char *name;
32 struct list_head drivers; 33 struct list_head drivers;
33 34
34 /* Default operations for these types of devices */ 35 /* Default operations for these types of devices */
diff --git a/include/linux/tifm.h b/include/linux/tifm.h
index 6b3a31805c72..2096b76d0cee 100644
--- a/include/linux/tifm.h
+++ b/include/linux/tifm.h
@@ -120,7 +120,7 @@ struct tifm_adapter {
120 struct completion *finish_me; 120 struct completion *finish_me;
121 121
122 struct work_struct media_switcher; 122 struct work_struct media_switcher;
123 struct class_device cdev; 123 struct device dev;
124 124
125 void (*eject)(struct tifm_adapter *fm, 125 void (*eject)(struct tifm_adapter *fm,
126 struct tifm_dev *sock); 126 struct tifm_dev *sock);
diff --git a/include/linux/uio_driver.h b/include/linux/uio_driver.h
index 44c28e94df50..973386d439da 100644
--- a/include/linux/uio_driver.h
+++ b/include/linux/uio_driver.h
@@ -18,20 +18,22 @@
18#include <linux/fs.h> 18#include <linux/fs.h>
19#include <linux/interrupt.h> 19#include <linux/interrupt.h>
20 20
21struct uio_map;
22
21/** 23/**
22 * struct uio_mem - description of a UIO memory region 24 * struct uio_mem - description of a UIO memory region
23 * @kobj: kobject for this mapping
24 * @addr: address of the device's memory 25 * @addr: address of the device's memory
25 * @size: size of IO 26 * @size: size of IO
26 * @memtype: type of memory addr points to 27 * @memtype: type of memory addr points to
27 * @internal_addr: ioremap-ped version of addr, for driver internal use 28 * @internal_addr: ioremap-ped version of addr, for driver internal use
29 * @map: for use by the UIO core only.
28 */ 30 */
29struct uio_mem { 31struct uio_mem {
30 struct kobject kobj;
31 unsigned long addr; 32 unsigned long addr;
32 unsigned long size; 33 unsigned long size;
33 int memtype; 34 int memtype;
34 void __iomem *internal_addr; 35 void __iomem *internal_addr;
36 struct uio_map *map;
35}; 37};
36 38
37#define MAX_UIO_MAPS 5 39#define MAX_UIO_MAPS 5
diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h
index 11f39606e7d9..cfbd38fe2998 100644
--- a/include/rdma/ib_verbs.h
+++ b/include/rdma/ib_verbs.h
@@ -1026,7 +1026,7 @@ struct ib_device {
1026 1026
1027 struct module *owner; 1027 struct module *owner;
1028 struct class_device class_dev; 1028 struct class_device class_dev;
1029 struct kobject ports_parent; 1029 struct kobject *ports_parent;
1030 struct list_head port_list; 1030 struct list_head port_list;
1031 1031
1032 enum { 1032 enum {
diff --git a/init/Kconfig b/init/Kconfig
index b9d11a899b88..f5becd2a12f6 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -363,6 +363,7 @@ config CGROUP_CPUACCT
363 363
364config SYSFS_DEPRECATED 364config SYSFS_DEPRECATED
365 bool "Create deprecated sysfs files" 365 bool "Create deprecated sysfs files"
366 depends on SYSFS
366 default y 367 default y
367 help 368 help
368 This option creates deprecated symlinks such as the 369 This option creates deprecated symlinks such as the
diff --git a/init/do_mounts.c b/init/do_mounts.c
index 4efa1e5385e3..2ae5b8462399 100644
--- a/init/do_mounts.c
+++ b/init/do_mounts.c
@@ -55,69 +55,6 @@ static int __init readwrite(char *str)
55__setup("ro", readonly); 55__setup("ro", readonly);
56__setup("rw", readwrite); 56__setup("rw", readwrite);
57 57
58static dev_t try_name(char *name, int part)
59{
60 char path[64];
61 char buf[32];
62 int range;
63 dev_t res;
64 char *s;
65 int len;
66 int fd;
67 unsigned int maj, min;
68
69 /* read device number from .../dev */
70
71 sprintf(path, "/sys/block/%s/dev", name);
72 fd = sys_open(path, 0, 0);
73 if (fd < 0)
74 goto fail;
75 len = sys_read(fd, buf, 32);
76 sys_close(fd);
77 if (len <= 0 || len == 32 || buf[len - 1] != '\n')
78 goto fail;
79 buf[len - 1] = '\0';
80 if (sscanf(buf, "%u:%u", &maj, &min) == 2) {
81 /*
82 * Try the %u:%u format -- see print_dev_t()
83 */
84 res = MKDEV(maj, min);
85 if (maj != MAJOR(res) || min != MINOR(res))
86 goto fail;
87 } else {
88 /*
89 * Nope. Try old-style "0321"
90 */
91 res = new_decode_dev(simple_strtoul(buf, &s, 16));
92 if (*s)
93 goto fail;
94 }
95
96 /* if it's there and we are not looking for a partition - that's it */
97 if (!part)
98 return res;
99
100 /* otherwise read range from .../range */
101 sprintf(path, "/sys/block/%s/range", name);
102 fd = sys_open(path, 0, 0);
103 if (fd < 0)
104 goto fail;
105 len = sys_read(fd, buf, 32);
106 sys_close(fd);
107 if (len <= 0 || len == 32 || buf[len - 1] != '\n')
108 goto fail;
109 buf[len - 1] = '\0';
110 range = simple_strtoul(buf, &s, 10);
111 if (*s)
112 goto fail;
113
114 /* if partition is within range - we got it */
115 if (part < range)
116 return res + part;
117fail:
118 return 0;
119}
120
121/* 58/*
122 * Convert a name into device number. We accept the following variants: 59 * Convert a name into device number. We accept the following variants:
123 * 60 *
@@ -129,12 +66,10 @@ fail:
129 * 5) /dev/<disk_name>p<decimal> - same as the above, that form is 66 * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
130 * used when disk name of partitioned disk ends on a digit. 67 * used when disk name of partitioned disk ends on a digit.
131 * 68 *
132 * If name doesn't have fall into the categories above, we return 0. 69 * If name doesn't have fall into the categories above, we return (0,0).
133 * Sysfs is used to check if something is a disk name - it has 70 * block_class is used to check if something is a disk name. If the disk
134 * all known disks under bus/block/devices. If the disk name 71 * name contains slashes, the device name has them replaced with
135 * contains slashes, name of sysfs node has them replaced with 72 * bangs.
136 * bangs. try_name() does the actual checks, assuming that sysfs
137 * is mounted on rootfs /sys.
138 */ 73 */
139 74
140dev_t name_to_dev_t(char *name) 75dev_t name_to_dev_t(char *name)
@@ -142,13 +77,6 @@ dev_t name_to_dev_t(char *name)
142 char s[32]; 77 char s[32];
143 char *p; 78 char *p;
144 dev_t res = 0; 79 dev_t res = 0;
145 int part;
146
147#ifdef CONFIG_SYSFS
148 int mkdir_err = sys_mkdir("/sys", 0700);
149 if (sys_mount("sysfs", "/sys", "sysfs", 0, NULL) < 0)
150 goto out;
151#endif
152 80
153 if (strncmp(name, "/dev/", 5) != 0) { 81 if (strncmp(name, "/dev/", 5) != 0) {
154 unsigned maj, min; 82 unsigned maj, min;
@@ -164,6 +92,7 @@ dev_t name_to_dev_t(char *name)
164 } 92 }
165 goto done; 93 goto done;
166 } 94 }
95
167 name += 5; 96 name += 5;
168 res = Root_NFS; 97 res = Root_NFS;
169 if (strcmp(name, "nfs") == 0) 98 if (strcmp(name, "nfs") == 0)
@@ -178,35 +107,14 @@ dev_t name_to_dev_t(char *name)
178 for (p = s; *p; p++) 107 for (p = s; *p; p++)
179 if (*p == '/') 108 if (*p == '/')
180 *p = '!'; 109 *p = '!';
181 res = try_name(s, 0); 110 res = blk_lookup_devt(s);
182 if (res) 111 if (res)
183 goto done; 112 goto done;
184 113
185 while (p > s && isdigit(p[-1])) 114fail:
186 p--; 115 return 0;
187 if (p == s || !*p || *p == '0')
188 goto fail;
189 part = simple_strtoul(p, NULL, 10);
190 *p = '\0';
191 res = try_name(s, part);
192 if (res)
193 goto done;
194
195 if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
196 goto fail;
197 p[-1] = '\0';
198 res = try_name(s, part);
199done: 116done:
200#ifdef CONFIG_SYSFS
201 sys_umount("/sys", 0);
202out:
203 if (!mkdir_err)
204 sys_rmdir("/sys");
205#endif
206 return res; 117 return res;
207fail:
208 res = 0;
209 goto done;
210} 118}
211 119
212static int __init root_dev_setup(char *line) 120static int __init root_dev_setup(char *line)
diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c
index 65daa5373ca6..e53bc30e9ba5 100644
--- a/kernel/ksysfs.c
+++ b/kernel/ksysfs.c
@@ -17,30 +17,34 @@
17#include <linux/sched.h> 17#include <linux/sched.h>
18 18
19#define KERNEL_ATTR_RO(_name) \ 19#define KERNEL_ATTR_RO(_name) \
20static struct subsys_attribute _name##_attr = __ATTR_RO(_name) 20static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
21 21
22#define KERNEL_ATTR_RW(_name) \ 22#define KERNEL_ATTR_RW(_name) \
23static struct subsys_attribute _name##_attr = \ 23static struct kobj_attribute _name##_attr = \
24 __ATTR(_name, 0644, _name##_show, _name##_store) 24 __ATTR(_name, 0644, _name##_show, _name##_store)
25 25
26#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET) 26#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
27/* current uevent sequence number */ 27/* current uevent sequence number */
28static ssize_t uevent_seqnum_show(struct kset *kset, char *page) 28static ssize_t uevent_seqnum_show(struct kobject *kobj,
29 struct kobj_attribute *attr, char *buf)
29{ 30{
30 return sprintf(page, "%llu\n", (unsigned long long)uevent_seqnum); 31 return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
31} 32}
32KERNEL_ATTR_RO(uevent_seqnum); 33KERNEL_ATTR_RO(uevent_seqnum);
33 34
34/* uevent helper program, used during early boo */ 35/* uevent helper program, used during early boo */
35static ssize_t uevent_helper_show(struct kset *kset, char *page) 36static ssize_t uevent_helper_show(struct kobject *kobj,
37 struct kobj_attribute *attr, char *buf)
36{ 38{
37 return sprintf(page, "%s\n", uevent_helper); 39 return sprintf(buf, "%s\n", uevent_helper);
38} 40}
39static ssize_t uevent_helper_store(struct kset *kset, const char *page, size_t count) 41static ssize_t uevent_helper_store(struct kobject *kobj,
42 struct kobj_attribute *attr,
43 const char *buf, size_t count)
40{ 44{
41 if (count+1 > UEVENT_HELPER_PATH_LEN) 45 if (count+1 > UEVENT_HELPER_PATH_LEN)
42 return -ENOENT; 46 return -ENOENT;
43 memcpy(uevent_helper, page, count); 47 memcpy(uevent_helper, buf, count);
44 uevent_helper[count] = '\0'; 48 uevent_helper[count] = '\0';
45 if (count && uevent_helper[count-1] == '\n') 49 if (count && uevent_helper[count-1] == '\n')
46 uevent_helper[count-1] = '\0'; 50 uevent_helper[count-1] = '\0';
@@ -50,21 +54,24 @@ KERNEL_ATTR_RW(uevent_helper);
50#endif 54#endif
51 55
52#ifdef CONFIG_KEXEC 56#ifdef CONFIG_KEXEC
53static ssize_t kexec_loaded_show(struct kset *kset, char *page) 57static ssize_t kexec_loaded_show(struct kobject *kobj,
58 struct kobj_attribute *attr, char *buf)
54{ 59{
55 return sprintf(page, "%d\n", !!kexec_image); 60 return sprintf(buf, "%d\n", !!kexec_image);
56} 61}
57KERNEL_ATTR_RO(kexec_loaded); 62KERNEL_ATTR_RO(kexec_loaded);
58 63
59static ssize_t kexec_crash_loaded_show(struct kset *kset, char *page) 64static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
65 struct kobj_attribute *attr, char *buf)
60{ 66{
61 return sprintf(page, "%d\n", !!kexec_crash_image); 67 return sprintf(buf, "%d\n", !!kexec_crash_image);
62} 68}
63KERNEL_ATTR_RO(kexec_crash_loaded); 69KERNEL_ATTR_RO(kexec_crash_loaded);
64 70
65static ssize_t vmcoreinfo_show(struct kset *kset, char *page) 71static ssize_t vmcoreinfo_show(struct kobject *kobj,
72 struct kobj_attribute *attr, char *buf)
66{ 73{
67 return sprintf(page, "%lx %x\n", 74 return sprintf(buf, "%lx %x\n",
68 paddr_vmcoreinfo_note(), 75 paddr_vmcoreinfo_note(),
69 (unsigned int)vmcoreinfo_max_size); 76 (unsigned int)vmcoreinfo_max_size);
70} 77}
@@ -94,8 +101,8 @@ static struct bin_attribute notes_attr = {
94 .read = &notes_read, 101 .read = &notes_read,
95}; 102};
96 103
97decl_subsys(kernel, NULL, NULL); 104struct kobject *kernel_kobj;
98EXPORT_SYMBOL_GPL(kernel_subsys); 105EXPORT_SYMBOL_GPL(kernel_kobj);
99 106
100static struct attribute * kernel_attrs[] = { 107static struct attribute * kernel_attrs[] = {
101#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET) 108#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
@@ -116,24 +123,39 @@ static struct attribute_group kernel_attr_group = {
116 123
117static int __init ksysfs_init(void) 124static int __init ksysfs_init(void)
118{ 125{
119 int error = subsystem_register(&kernel_subsys); 126 int error;
120 if (!error)
121 error = sysfs_create_group(&kernel_subsys.kobj,
122 &kernel_attr_group);
123 127
124 if (!error && notes_size > 0) { 128 kernel_kobj = kobject_create_and_add("kernel", NULL);
125 notes_attr.size = notes_size; 129 if (!kernel_kobj) {
126 error = sysfs_create_bin_file(&kernel_subsys.kobj, 130 error = -ENOMEM;
127 &notes_attr); 131 goto exit;
128 } 132 }
133 error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
134 if (error)
135 goto kset_exit;
129 136
130 /* 137 if (notes_size > 0) {
131 * Create "/sys/kernel/uids" directory and corresponding root user's 138 notes_attr.size = notes_size;
132 * directory under it. 139 error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
133 */ 140 if (error)
134 if (!error) 141 goto group_exit;
135 error = uids_kobject_init(); 142 }
136 143
144 /* create the /sys/kernel/uids/ directory */
145 error = uids_sysfs_init();
146 if (error)
147 goto notes_exit;
148
149 return 0;
150
151notes_exit:
152 if (notes_size > 0)
153 sysfs_remove_bin_file(kernel_kobj, &notes_attr);
154group_exit:
155 sysfs_remove_group(kernel_kobj, &kernel_attr_group);
156kset_exit:
157 kobject_put(kernel_kobj);
158exit:
137 return error; 159 return error;
138} 160}
139 161
diff --git a/kernel/module.c b/kernel/module.c
index c2e3e2e98801..dcb8a2cbf75e 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -47,8 +47,6 @@
47#include <asm/cacheflush.h> 47#include <asm/cacheflush.h>
48#include <linux/license.h> 48#include <linux/license.h>
49 49
50extern int module_sysfs_initialized;
51
52#if 0 50#if 0
53#define DEBUGP printk 51#define DEBUGP printk
54#else 52#else
@@ -1122,7 +1120,7 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1122 ++loaded; 1120 ++loaded;
1123 } 1121 }
1124 1122
1125 notes_attrs->dir = kobject_add_dir(&mod->mkobj.kobj, "notes"); 1123 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1126 if (!notes_attrs->dir) 1124 if (!notes_attrs->dir)
1127 goto out; 1125 goto out;
1128 1126
@@ -1219,15 +1217,16 @@ int mod_sysfs_init(struct module *mod)
1219 err = -EINVAL; 1217 err = -EINVAL;
1220 goto out; 1218 goto out;
1221 } 1219 }
1222 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1223 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1224 if (err)
1225 goto out;
1226 kobj_set_kset_s(&mod->mkobj, module_subsys);
1227 mod->mkobj.mod = mod; 1220 mod->mkobj.mod = mod;
1228 1221
1229 kobject_init(&mod->mkobj.kobj); 1222 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1223 mod->mkobj.kobj.kset = module_kset;
1224 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1225 "%s", mod->name);
1226 if (err)
1227 kobject_put(&mod->mkobj.kobj);
1230 1228
1229 /* delay uevent until full sysfs population */
1231out: 1230out:
1232 return err; 1231 return err;
1233} 1232}
@@ -1238,12 +1237,7 @@ int mod_sysfs_setup(struct module *mod,
1238{ 1237{
1239 int err; 1238 int err;
1240 1239
1241 /* delay uevent until full sysfs population */ 1240 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1242 err = kobject_add(&mod->mkobj.kobj);
1243 if (err)
1244 goto out;
1245
1246 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
1247 if (!mod->holders_dir) { 1241 if (!mod->holders_dir) {
1248 err = -ENOMEM; 1242 err = -ENOMEM;
1249 goto out_unreg; 1243 goto out_unreg;
@@ -1263,11 +1257,9 @@ int mod_sysfs_setup(struct module *mod,
1263out_unreg_param: 1257out_unreg_param:
1264 module_param_sysfs_remove(mod); 1258 module_param_sysfs_remove(mod);
1265out_unreg_holders: 1259out_unreg_holders:
1266 kobject_unregister(mod->holders_dir); 1260 kobject_put(mod->holders_dir);
1267out_unreg: 1261out_unreg:
1268 kobject_del(&mod->mkobj.kobj);
1269 kobject_put(&mod->mkobj.kobj); 1262 kobject_put(&mod->mkobj.kobj);
1270out:
1271 return err; 1263 return err;
1272} 1264}
1273#endif 1265#endif
@@ -1276,9 +1268,9 @@ static void mod_kobject_remove(struct module *mod)
1276{ 1268{
1277 module_remove_modinfo_attrs(mod); 1269 module_remove_modinfo_attrs(mod);
1278 module_param_sysfs_remove(mod); 1270 module_param_sysfs_remove(mod);
1279 kobject_unregister(mod->mkobj.drivers_dir); 1271 kobject_put(mod->mkobj.drivers_dir);
1280 kobject_unregister(mod->holders_dir); 1272 kobject_put(mod->holders_dir);
1281 kobject_unregister(&mod->mkobj.kobj); 1273 kobject_put(&mod->mkobj.kobj);
1282} 1274}
1283 1275
1284/* 1276/*
@@ -1884,10 +1876,10 @@ static struct module *load_module(void __user *umod,
1884 /* Now we've moved module, initialize linked lists, etc. */ 1876 /* Now we've moved module, initialize linked lists, etc. */
1885 module_unload_init(mod); 1877 module_unload_init(mod);
1886 1878
1887 /* Initialize kobject, so we can reference it. */ 1879 /* add kobject, so we can reference it. */
1888 err = mod_sysfs_init(mod); 1880 err = mod_sysfs_init(mod);
1889 if (err) 1881 if (err)
1890 goto cleanup; 1882 goto free_unload;
1891 1883
1892 /* Set up license info based on the info section */ 1884 /* Set up license info based on the info section */
1893 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 1885 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
@@ -2057,6 +2049,9 @@ static struct module *load_module(void __user *umod,
2057 arch_cleanup: 2049 arch_cleanup:
2058 module_arch_cleanup(mod); 2050 module_arch_cleanup(mod);
2059 cleanup: 2051 cleanup:
2052 kobject_del(&mod->mkobj.kobj);
2053 kobject_put(&mod->mkobj.kobj);
2054 free_unload:
2060 module_unload_free(mod); 2055 module_unload_free(mod);
2061 module_free(mod, mod->module_init); 2056 module_free(mod, mod->module_init);
2062 free_core: 2057 free_core:
@@ -2502,93 +2497,6 @@ void print_modules(void)
2502 printk("\n"); 2497 printk("\n");
2503} 2498}
2504 2499
2505#ifdef CONFIG_SYSFS
2506static char *make_driver_name(struct device_driver *drv)
2507{
2508 char *driver_name;
2509
2510 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2511 GFP_KERNEL);
2512 if (!driver_name)
2513 return NULL;
2514
2515 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2516 return driver_name;
2517}
2518
2519static void module_create_drivers_dir(struct module_kobject *mk)
2520{
2521 if (!mk || mk->drivers_dir)
2522 return;
2523
2524 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2525}
2526
2527void module_add_driver(struct module *mod, struct device_driver *drv)
2528{
2529 char *driver_name;
2530 int no_warn;
2531 struct module_kobject *mk = NULL;
2532
2533 if (!drv)
2534 return;
2535
2536 if (mod)
2537 mk = &mod->mkobj;
2538 else if (drv->mod_name) {
2539 struct kobject *mkobj;
2540
2541 /* Lookup built-in module entry in /sys/modules */
2542 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
2543 if (mkobj) {
2544 mk = container_of(mkobj, struct module_kobject, kobj);
2545 /* remember our module structure */
2546 drv->mkobj = mk;
2547 /* kset_find_obj took a reference */
2548 kobject_put(mkobj);
2549 }
2550 }
2551
2552 if (!mk)
2553 return;
2554
2555 /* Don't check return codes; these calls are idempotent */
2556 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
2557 driver_name = make_driver_name(drv);
2558 if (driver_name) {
2559 module_create_drivers_dir(mk);
2560 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
2561 driver_name);
2562 kfree(driver_name);
2563 }
2564}
2565EXPORT_SYMBOL(module_add_driver);
2566
2567void module_remove_driver(struct device_driver *drv)
2568{
2569 struct module_kobject *mk = NULL;
2570 char *driver_name;
2571
2572 if (!drv)
2573 return;
2574
2575 sysfs_remove_link(&drv->kobj, "module");
2576
2577 if (drv->owner)
2578 mk = &drv->owner->mkobj;
2579 else if (drv->mkobj)
2580 mk = drv->mkobj;
2581 if (mk && mk->drivers_dir) {
2582 driver_name = make_driver_name(drv);
2583 if (driver_name) {
2584 sysfs_remove_link(mk->drivers_dir, driver_name);
2585 kfree(driver_name);
2586 }
2587 }
2588}
2589EXPORT_SYMBOL(module_remove_driver);
2590#endif
2591
2592#ifdef CONFIG_MODVERSIONS 2500#ifdef CONFIG_MODVERSIONS
2593/* Generate the signature for struct module here, too, for modversions. */ 2501/* Generate the signature for struct module here, too, for modversions. */
2594void struct_module(struct module *mod) { return; } 2502void struct_module(struct module *mod) { return; }
diff --git a/kernel/params.c b/kernel/params.c
index 7686417ee00e..b4da9505f4d2 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -560,11 +560,10 @@ static void __init kernel_param_sysfs_setup(const char *name,
560 BUG_ON(!mk); 560 BUG_ON(!mk);
561 561
562 mk->mod = THIS_MODULE; 562 mk->mod = THIS_MODULE;
563 kobj_set_kset_s(mk, module_subsys); 563 mk->kobj.kset = module_kset;
564 kobject_set_name(&mk->kobj, name); 564 ret = kobject_init_and_add(&mk->kobj, &module_ktype, NULL, "%s", name);
565 kobject_init(&mk->kobj);
566 ret = kobject_add(&mk->kobj);
567 if (ret) { 565 if (ret) {
566 kobject_put(&mk->kobj);
568 printk(KERN_ERR "Module '%s' failed to be added to sysfs, " 567 printk(KERN_ERR "Module '%s' failed to be added to sysfs, "
569 "error number %d\n", name, ret); 568 "error number %d\n", name, ret);
570 printk(KERN_ERR "The system will be unstable now.\n"); 569 printk(KERN_ERR "The system will be unstable now.\n");
@@ -679,8 +678,6 @@ static struct sysfs_ops module_sysfs_ops = {
679 .store = module_attr_store, 678 .store = module_attr_store,
680}; 679};
681 680
682static struct kobj_type module_ktype;
683
684static int uevent_filter(struct kset *kset, struct kobject *kobj) 681static int uevent_filter(struct kset *kset, struct kobject *kobj)
685{ 682{
686 struct kobj_type *ktype = get_ktype(kobj); 683 struct kobj_type *ktype = get_ktype(kobj);
@@ -694,21 +691,11 @@ static struct kset_uevent_ops module_uevent_ops = {
694 .filter = uevent_filter, 691 .filter = uevent_filter,
695}; 692};
696 693
697decl_subsys(module, &module_ktype, &module_uevent_ops); 694struct kset *module_kset;
698int module_sysfs_initialized; 695int module_sysfs_initialized;
699 696
700static void module_release(struct kobject *kobj) 697struct kobj_type module_ktype = {
701{
702 /*
703 * Stupid empty release function to allow the memory for the kobject to
704 * be properly cleaned up. This will not need to be present for 2.6.25
705 * with the upcoming kobject core rework.
706 */
707}
708
709static struct kobj_type module_ktype = {
710 .sysfs_ops = &module_sysfs_ops, 698 .sysfs_ops = &module_sysfs_ops,
711 .release = module_release,
712}; 699};
713 700
714/* 701/*
@@ -716,13 +703,11 @@ static struct kobj_type module_ktype = {
716 */ 703 */
717static int __init param_sysfs_init(void) 704static int __init param_sysfs_init(void)
718{ 705{
719 int ret; 706 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
720 707 if (!module_kset) {
721 ret = subsystem_register(&module_subsys); 708 printk(KERN_WARNING "%s (%d): error creating kset\n",
722 if (ret < 0) { 709 __FILE__, __LINE__);
723 printk(KERN_WARNING "%s (%d): subsystem_register error: %d\n", 710 return -ENOMEM;
724 __FILE__, __LINE__, ret);
725 return ret;
726 } 711 }
727 module_sysfs_initialized = 1; 712 module_sysfs_initialized = 1;
728 713
@@ -732,14 +717,7 @@ static int __init param_sysfs_init(void)
732} 717}
733subsys_initcall(param_sysfs_init); 718subsys_initcall(param_sysfs_init);
734 719
735#else 720#endif /* CONFIG_SYSFS */
736#if 0
737static struct sysfs_ops module_sysfs_ops = {
738 .show = NULL,
739 .store = NULL,
740};
741#endif
742#endif
743 721
744EXPORT_SYMBOL(param_set_byte); 722EXPORT_SYMBOL(param_set_byte);
745EXPORT_SYMBOL(param_get_byte); 723EXPORT_SYMBOL(param_get_byte);
diff --git a/kernel/power/disk.c b/kernel/power/disk.c
index 05b64790fe83..b138b431e271 100644
--- a/kernel/power/disk.c
+++ b/kernel/power/disk.c
@@ -567,7 +567,8 @@ static const char * const hibernation_modes[] = {
567 * supports it (as determined by having hibernation_ops). 567 * supports it (as determined by having hibernation_ops).
568 */ 568 */
569 569
570static ssize_t disk_show(struct kset *kset, char *buf) 570static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
571 char *buf)
571{ 572{
572 int i; 573 int i;
573 char *start = buf; 574 char *start = buf;
@@ -597,7 +598,8 @@ static ssize_t disk_show(struct kset *kset, char *buf)
597} 598}
598 599
599 600
600static ssize_t disk_store(struct kset *kset, const char *buf, size_t n) 601static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
602 const char *buf, size_t n)
601{ 603{
602 int error = 0; 604 int error = 0;
603 int i; 605 int i;
@@ -642,13 +644,15 @@ static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
642 644
643power_attr(disk); 645power_attr(disk);
644 646
645static ssize_t resume_show(struct kset *kset, char *buf) 647static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
648 char *buf)
646{ 649{
647 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), 650 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
648 MINOR(swsusp_resume_device)); 651 MINOR(swsusp_resume_device));
649} 652}
650 653
651static ssize_t resume_store(struct kset *kset, const char *buf, size_t n) 654static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
655 const char *buf, size_t n)
652{ 656{
653 unsigned int maj, min; 657 unsigned int maj, min;
654 dev_t res; 658 dev_t res;
@@ -674,12 +678,14 @@ static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
674 678
675power_attr(resume); 679power_attr(resume);
676 680
677static ssize_t image_size_show(struct kset *kset, char *buf) 681static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
682 char *buf)
678{ 683{
679 return sprintf(buf, "%lu\n", image_size); 684 return sprintf(buf, "%lu\n", image_size);
680} 685}
681 686
682static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n) 687static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
688 const char *buf, size_t n)
683{ 689{
684 unsigned long size; 690 unsigned long size;
685 691
@@ -708,7 +714,7 @@ static struct attribute_group attr_group = {
708 714
709static int __init pm_disk_init(void) 715static int __init pm_disk_init(void)
710{ 716{
711 return sysfs_create_group(&power_subsys.kobj, &attr_group); 717 return sysfs_create_group(power_kobj, &attr_group);
712} 718}
713 719
714core_initcall(pm_disk_init); 720core_initcall(pm_disk_init);
diff --git a/kernel/power/main.c b/kernel/power/main.c
index f71c9504a5c5..efc08360e627 100644
--- a/kernel/power/main.c
+++ b/kernel/power/main.c
@@ -276,8 +276,7 @@ EXPORT_SYMBOL(pm_suspend);
276 276
277#endif /* CONFIG_SUSPEND */ 277#endif /* CONFIG_SUSPEND */
278 278
279decl_subsys(power,NULL,NULL); 279struct kobject *power_kobj;
280
281 280
282/** 281/**
283 * state - control system power state. 282 * state - control system power state.
@@ -290,7 +289,8 @@ decl_subsys(power,NULL,NULL);
290 * proper enumerated value, and initiates a suspend transition. 289 * proper enumerated value, and initiates a suspend transition.
291 */ 290 */
292 291
293static ssize_t state_show(struct kset *kset, char *buf) 292static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
293 char *buf)
294{ 294{
295 char *s = buf; 295 char *s = buf;
296#ifdef CONFIG_SUSPEND 296#ifdef CONFIG_SUSPEND
@@ -311,7 +311,8 @@ static ssize_t state_show(struct kset *kset, char *buf)
311 return (s - buf); 311 return (s - buf);
312} 312}
313 313
314static ssize_t state_store(struct kset *kset, const char *buf, size_t n) 314static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
315 const char *buf, size_t n)
315{ 316{
316#ifdef CONFIG_SUSPEND 317#ifdef CONFIG_SUSPEND
317 suspend_state_t state = PM_SUSPEND_STANDBY; 318 suspend_state_t state = PM_SUSPEND_STANDBY;
@@ -348,13 +349,15 @@ power_attr(state);
348#ifdef CONFIG_PM_TRACE 349#ifdef CONFIG_PM_TRACE
349int pm_trace_enabled; 350int pm_trace_enabled;
350 351
351static ssize_t pm_trace_show(struct kset *kset, char *buf) 352static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
353 char *buf)
352{ 354{
353 return sprintf(buf, "%d\n", pm_trace_enabled); 355 return sprintf(buf, "%d\n", pm_trace_enabled);
354} 356}
355 357
356static ssize_t 358static ssize_t
357pm_trace_store(struct kset *kset, const char *buf, size_t n) 359pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
360 const char *buf, size_t n)
358{ 361{
359 int val; 362 int val;
360 363
@@ -386,10 +389,10 @@ static struct attribute_group attr_group = {
386 389
387static int __init pm_init(void) 390static int __init pm_init(void)
388{ 391{
389 int error = subsystem_register(&power_subsys); 392 power_kobj = kobject_create_and_add("power", NULL);
390 if (!error) 393 if (!power_kobj)
391 error = sysfs_create_group(&power_subsys.kobj,&attr_group); 394 return -ENOMEM;
392 return error; 395 return sysfs_create_group(power_kobj, &attr_group);
393} 396}
394 397
395core_initcall(pm_init); 398core_initcall(pm_init);
diff --git a/kernel/power/power.h b/kernel/power/power.h
index 195dc4611764..2093c3a9a994 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -54,7 +54,7 @@ extern int pfn_is_nosave(unsigned long);
54extern struct mutex pm_mutex; 54extern struct mutex pm_mutex;
55 55
56#define power_attr(_name) \ 56#define power_attr(_name) \
57static struct subsys_attribute _name##_attr = { \ 57static struct kobj_attribute _name##_attr = { \
58 .attr = { \ 58 .attr = { \
59 .name = __stringify(_name), \ 59 .name = __stringify(_name), \
60 .mode = 0644, \ 60 .mode = 0644, \
@@ -63,8 +63,6 @@ static struct subsys_attribute _name##_attr = { \
63 .store = _name##_store, \ 63 .store = _name##_store, \
64} 64}
65 65
66extern struct kset power_subsys;
67
68/* Preferred image size in bytes (default 500 MB) */ 66/* Preferred image size in bytes (default 500 MB) */
69extern unsigned long image_size; 67extern unsigned long image_size;
70extern int in_suspend; 68extern int in_suspend;
diff --git a/kernel/rtmutex-tester.c b/kernel/rtmutex-tester.c
index e3055ba69159..092e4c620af9 100644
--- a/kernel/rtmutex-tester.c
+++ b/kernel/rtmutex-tester.c
@@ -394,7 +394,7 @@ static SYSDEV_ATTR(status, 0600, sysfs_test_status, NULL);
394static SYSDEV_ATTR(command, 0600, NULL, sysfs_test_command); 394static SYSDEV_ATTR(command, 0600, NULL, sysfs_test_command);
395 395
396static struct sysdev_class rttest_sysclass = { 396static struct sysdev_class rttest_sysclass = {
397 set_kset_name("rttest"), 397 .name = "rttest",
398}; 398};
399 399
400static int init_test_thread(int id) 400static int init_test_thread(int id)
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index c8a9d13874df..8d6125ad2cf0 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -441,7 +441,7 @@ static SYSDEV_ATTR(available_clocksource, 0600,
441 sysfs_show_available_clocksources, NULL); 441 sysfs_show_available_clocksources, NULL);
442 442
443static struct sysdev_class clocksource_sysclass = { 443static struct sysdev_class clocksource_sysclass = {
444 set_kset_name("clocksource"), 444 .name = "clocksource",
445}; 445};
446 446
447static struct sys_device device_clocksource = { 447static struct sys_device device_clocksource = {
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index e5e466b27598..ab46ae8c062b 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -335,9 +335,9 @@ static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
335 335
336/* sysfs resume/suspend bits for timekeeping */ 336/* sysfs resume/suspend bits for timekeeping */
337static struct sysdev_class timekeeping_sysclass = { 337static struct sysdev_class timekeeping_sysclass = {
338 .name = "timekeeping",
338 .resume = timekeeping_resume, 339 .resume = timekeeping_resume,
339 .suspend = timekeeping_suspend, 340 .suspend = timekeeping_suspend,
340 set_kset_name("timekeeping"),
341}; 341};
342 342
343static struct sys_device device_timer = { 343static struct sys_device device_timer = {
diff --git a/kernel/user.c b/kernel/user.c
index 8320a87f3e5a..ab4fd706993b 100644
--- a/kernel/user.c
+++ b/kernel/user.c
@@ -115,7 +115,7 @@ static void sched_switch_user(struct task_struct *p) { }
115 115
116#if defined(CONFIG_FAIR_USER_SCHED) && defined(CONFIG_SYSFS) 116#if defined(CONFIG_FAIR_USER_SCHED) && defined(CONFIG_SYSFS)
117 117
118static struct kobject uids_kobject; /* represents /sys/kernel/uids directory */ 118static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */
119static DEFINE_MUTEX(uids_mutex); 119static DEFINE_MUTEX(uids_mutex);
120 120
121static inline void uids_mutex_lock(void) 121static inline void uids_mutex_lock(void)
@@ -128,86 +128,83 @@ static inline void uids_mutex_unlock(void)
128 mutex_unlock(&uids_mutex); 128 mutex_unlock(&uids_mutex);
129} 129}
130 130
131/* return cpu shares held by the user */ 131/* uid directory attributes */
132static ssize_t cpu_shares_show(struct kset *kset, char *buffer) 132static ssize_t cpu_shares_show(struct kobject *kobj,
133 struct kobj_attribute *attr,
134 char *buf)
133{ 135{
134 struct user_struct *up = container_of(kset, struct user_struct, kset); 136 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
135 137
136 return sprintf(buffer, "%lu\n", sched_group_shares(up->tg)); 138 return sprintf(buf, "%lu\n", sched_group_shares(up->tg));
137} 139}
138 140
139/* modify cpu shares held by the user */ 141static ssize_t cpu_shares_store(struct kobject *kobj,
140static ssize_t cpu_shares_store(struct kset *kset, const char *buffer, 142 struct kobj_attribute *attr,
141 size_t size) 143 const char *buf, size_t size)
142{ 144{
143 struct user_struct *up = container_of(kset, struct user_struct, kset); 145 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
144 unsigned long shares; 146 unsigned long shares;
145 int rc; 147 int rc;
146 148
147 sscanf(buffer, "%lu", &shares); 149 sscanf(buf, "%lu", &shares);
148 150
149 rc = sched_group_set_shares(up->tg, shares); 151 rc = sched_group_set_shares(up->tg, shares);
150 152
151 return (rc ? rc : size); 153 return (rc ? rc : size);
152} 154}
153 155
154static void user_attr_init(struct subsys_attribute *sa, char *name, int mode) 156static struct kobj_attribute cpu_share_attr =
157 __ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
158
159/* default attributes per uid directory */
160static struct attribute *uids_attributes[] = {
161 &cpu_share_attr.attr,
162 NULL
163};
164
165/* the lifetime of user_struct is not managed by the core (now) */
166static void uids_release(struct kobject *kobj)
155{ 167{
156 sa->attr.name = name; 168 return;
157 sa->attr.mode = mode;
158 sa->show = cpu_shares_show;
159 sa->store = cpu_shares_store;
160} 169}
161 170
162/* Create "/sys/kernel/uids/<uid>" directory and 171static struct kobj_type uids_ktype = {
163 * "/sys/kernel/uids/<uid>/cpu_share" file for this user. 172 .sysfs_ops = &kobj_sysfs_ops,
164 */ 173 .default_attrs = uids_attributes,
165static int user_kobject_create(struct user_struct *up) 174 .release = uids_release,
175};
176
177/* create /sys/kernel/uids/<uid>/cpu_share file for this user */
178static int uids_user_create(struct user_struct *up)
166{ 179{
167 struct kset *kset = &up->kset; 180 struct kobject *kobj = &up->kobj;
168 struct kobject *kobj = &kset->kobj;
169 int error; 181 int error;
170 182
171 memset(kset, 0, sizeof(struct kset)); 183 memset(kobj, 0, sizeof(struct kobject));
172 kobj->parent = &uids_kobject; /* create under /sys/kernel/uids dir */ 184 kobj->kset = uids_kset;
173 kobject_set_name(kobj, "%d", up->uid); 185 error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
174 kset_init(kset); 186 if (error) {
175 user_attr_init(&up->user_attr, "cpu_share", 0644); 187 kobject_put(kobj);
176
177 error = kobject_add(kobj);
178 if (error)
179 goto done; 188 goto done;
180 189 }
181 error = sysfs_create_file(kobj, &up->user_attr.attr);
182 if (error)
183 kobject_del(kobj);
184 190
185 kobject_uevent(kobj, KOBJ_ADD); 191 kobject_uevent(kobj, KOBJ_ADD);
186
187done: 192done:
188 return error; 193 return error;
189} 194}
190 195
191/* create these in sysfs filesystem: 196/* create these entries in sysfs:
192 * "/sys/kernel/uids" directory 197 * "/sys/kernel/uids" directory
193 * "/sys/kernel/uids/0" directory (for root user) 198 * "/sys/kernel/uids/0" directory (for root user)
194 * "/sys/kernel/uids/0/cpu_share" file (for root user) 199 * "/sys/kernel/uids/0/cpu_share" file (for root user)
195 */ 200 */
196int __init uids_kobject_init(void) 201int __init uids_sysfs_init(void)
197{ 202{
198 int error; 203 uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
204 if (!uids_kset)
205 return -ENOMEM;
199 206
200 /* create under /sys/kernel dir */ 207 return uids_user_create(&root_user);
201 uids_kobject.parent = &kernel_subsys.kobj;
202 uids_kobject.kset = &kernel_subsys;
203 kobject_set_name(&uids_kobject, "uids");
204 kobject_init(&uids_kobject);
205
206 error = kobject_add(&uids_kobject);
207 if (!error)
208 error = user_kobject_create(&root_user);
209
210 return error;
211} 208}
212 209
213/* work function to remove sysfs directory for a user and free up 210/* work function to remove sysfs directory for a user and free up
@@ -216,7 +213,6 @@ int __init uids_kobject_init(void)
216static void remove_user_sysfs_dir(struct work_struct *w) 213static void remove_user_sysfs_dir(struct work_struct *w)
217{ 214{
218 struct user_struct *up = container_of(w, struct user_struct, work); 215 struct user_struct *up = container_of(w, struct user_struct, work);
219 struct kobject *kobj = &up->kset.kobj;
220 unsigned long flags; 216 unsigned long flags;
221 int remove_user = 0; 217 int remove_user = 0;
222 218
@@ -238,9 +234,9 @@ static void remove_user_sysfs_dir(struct work_struct *w)
238 if (!remove_user) 234 if (!remove_user)
239 goto done; 235 goto done;
240 236
241 sysfs_remove_file(kobj, &up->user_attr.attr); 237 kobject_uevent(&up->kobj, KOBJ_REMOVE);
242 kobject_uevent(kobj, KOBJ_REMOVE); 238 kobject_del(&up->kobj);
243 kobject_del(kobj); 239 kobject_put(&up->kobj);
244 240
245 sched_destroy_user(up); 241 sched_destroy_user(up);
246 key_put(up->uid_keyring); 242 key_put(up->uid_keyring);
@@ -267,7 +263,8 @@ static inline void free_user(struct user_struct *up, unsigned long flags)
267 263
268#else /* CONFIG_FAIR_USER_SCHED && CONFIG_SYSFS */ 264#else /* CONFIG_FAIR_USER_SCHED && CONFIG_SYSFS */
269 265
270static inline int user_kobject_create(struct user_struct *up) { return 0; } 266int uids_sysfs_init(void) { return 0; }
267static inline int uids_user_create(struct user_struct *up) { return 0; }
271static inline void uids_mutex_lock(void) { } 268static inline void uids_mutex_lock(void) { }
272static inline void uids_mutex_unlock(void) { } 269static inline void uids_mutex_unlock(void) { }
273 270
@@ -324,7 +321,7 @@ struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
324 struct hlist_head *hashent = uidhashentry(ns, uid); 321 struct hlist_head *hashent = uidhashentry(ns, uid);
325 struct user_struct *up; 322 struct user_struct *up;
326 323
327 /* Make uid_hash_find() + user_kobject_create() + uid_hash_insert() 324 /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
328 * atomic. 325 * atomic.
329 */ 326 */
330 uids_mutex_lock(); 327 uids_mutex_lock();
@@ -370,7 +367,7 @@ struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
370 return NULL; 367 return NULL;
371 } 368 }
372 369
373 if (user_kobject_create(new)) { 370 if (uids_user_create(new)) {
374 sched_destroy_user(new); 371 sched_destroy_user(new);
375 key_put(new->uid_keyring); 372 key_put(new->uid_keyring);
376 key_put(new->session_keyring); 373 key_put(new->session_keyring);
diff --git a/lib/kobject.c b/lib/kobject.c
index 3590f022a609..1d63ead1815e 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -18,58 +18,57 @@
18#include <linux/stat.h> 18#include <linux/stat.h>
19#include <linux/slab.h> 19#include <linux/slab.h>
20 20
21/** 21/*
22 * populate_dir - populate directory with attributes. 22 * populate_dir - populate directory with attributes.
23 * @kobj: object we're working on. 23 * @kobj: object we're working on.
24 *
25 * Most subsystems have a set of default attributes that
26 * are associated with an object that registers with them.
27 * This is a helper called during object registration that
28 * loops through the default attributes of the subsystem
29 * and creates attributes files for them in sysfs.
30 * 24 *
25 * Most subsystems have a set of default attributes that are associated
26 * with an object that registers with them. This is a helper called during
27 * object registration that loops through the default attributes of the
28 * subsystem and creates attributes files for them in sysfs.
31 */ 29 */
32 30static int populate_dir(struct kobject *kobj)
33static int populate_dir(struct kobject * kobj)
34{ 31{
35 struct kobj_type * t = get_ktype(kobj); 32 struct kobj_type *t = get_ktype(kobj);
36 struct attribute * attr; 33 struct attribute *attr;
37 int error = 0; 34 int error = 0;
38 int i; 35 int i;
39 36
40 if (t && t->default_attrs) { 37 if (t && t->default_attrs) {
41 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) { 38 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
42 if ((error = sysfs_create_file(kobj,attr))) 39 error = sysfs_create_file(kobj, attr);
40 if (error)
43 break; 41 break;
44 } 42 }
45 } 43 }
46 return error; 44 return error;
47} 45}
48 46
49static int create_dir(struct kobject * kobj) 47static int create_dir(struct kobject *kobj)
50{ 48{
51 int error = 0; 49 int error = 0;
52 if (kobject_name(kobj)) { 50 if (kobject_name(kobj)) {
53 error = sysfs_create_dir(kobj); 51 error = sysfs_create_dir(kobj);
54 if (!error) { 52 if (!error) {
55 if ((error = populate_dir(kobj))) 53 error = populate_dir(kobj);
54 if (error)
56 sysfs_remove_dir(kobj); 55 sysfs_remove_dir(kobj);
57 } 56 }
58 } 57 }
59 return error; 58 return error;
60} 59}
61 60
62static inline struct kobject * to_kobj(struct list_head * entry) 61static inline struct kobject *to_kobj(struct list_head *entry)
63{ 62{
64 return container_of(entry,struct kobject,entry); 63 return container_of(entry, struct kobject, entry);
65} 64}
66 65
67static int get_kobj_path_length(struct kobject *kobj) 66static int get_kobj_path_length(struct kobject *kobj)
68{ 67{
69 int length = 1; 68 int length = 1;
70 struct kobject * parent = kobj; 69 struct kobject *parent = kobj;
71 70
72 /* walk up the ancestors until we hit the one pointing to the 71 /* walk up the ancestors until we hit the one pointing to the
73 * root. 72 * root.
74 * Add 1 to strlen for leading '/' of each level. 73 * Add 1 to strlen for leading '/' of each level.
75 */ 74 */
@@ -84,18 +83,19 @@ static int get_kobj_path_length(struct kobject *kobj)
84 83
85static void fill_kobj_path(struct kobject *kobj, char *path, int length) 84static void fill_kobj_path(struct kobject *kobj, char *path, int length)
86{ 85{
87 struct kobject * parent; 86 struct kobject *parent;
88 87
89 --length; 88 --length;
90 for (parent = kobj; parent; parent = parent->parent) { 89 for (parent = kobj; parent; parent = parent->parent) {
91 int cur = strlen(kobject_name(parent)); 90 int cur = strlen(kobject_name(parent));
92 /* back up enough to print this name with '/' */ 91 /* back up enough to print this name with '/' */
93 length -= cur; 92 length -= cur;
94 strncpy (path + length, kobject_name(parent), cur); 93 strncpy(path + length, kobject_name(parent), cur);
95 *(path + --length) = '/'; 94 *(path + --length) = '/';
96 } 95 }
97 96
98 pr_debug("%s: path = '%s'\n",__FUNCTION__,path); 97 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
98 kobj, __FUNCTION__, path);
99} 99}
100 100
101/** 101/**
@@ -123,179 +123,286 @@ char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
123} 123}
124EXPORT_SYMBOL_GPL(kobject_get_path); 124EXPORT_SYMBOL_GPL(kobject_get_path);
125 125
126/** 126/* add the kobject to its kset's list */
127 * kobject_init - initialize object. 127static void kobj_kset_join(struct kobject *kobj)
128 * @kobj: object in question.
129 */
130void kobject_init(struct kobject * kobj)
131{ 128{
132 if (!kobj) 129 if (!kobj->kset)
133 return; 130 return;
134 kref_init(&kobj->kref); 131
135 INIT_LIST_HEAD(&kobj->entry); 132 kset_get(kobj->kset);
136 kobj->kset = kset_get(kobj->kset); 133 spin_lock(&kobj->kset->list_lock);
134 list_add_tail(&kobj->entry, &kobj->kset->list);
135 spin_unlock(&kobj->kset->list_lock);
137} 136}
138 137
138/* remove the kobject from its kset's list */
139static void kobj_kset_leave(struct kobject *kobj)
140{
141 if (!kobj->kset)
142 return;
139 143
140/** 144 spin_lock(&kobj->kset->list_lock);
141 * unlink - remove kobject from kset list. 145 list_del_init(&kobj->entry);
142 * @kobj: kobject. 146 spin_unlock(&kobj->kset->list_lock);
143 * 147 kset_put(kobj->kset);
144 * Remove the kobject from the kset list and decrement 148}
145 * its parent's refcount.
146 * This is separated out, so we can use it in both
147 * kobject_del() and kobject_add() on error.
148 */
149 149
150static void unlink(struct kobject * kobj) 150static void kobject_init_internal(struct kobject *kobj)
151{ 151{
152 if (kobj->kset) { 152 if (!kobj)
153 spin_lock(&kobj->kset->list_lock); 153 return;
154 list_del_init(&kobj->entry); 154 kref_init(&kobj->kref);
155 spin_unlock(&kobj->kset->list_lock); 155 INIT_LIST_HEAD(&kobj->entry);
156 }
157 kobject_put(kobj);
158} 156}
159 157
160/**
161 * kobject_add - add an object to the hierarchy.
162 * @kobj: object.
163 */
164 158
165int kobject_add(struct kobject * kobj) 159static int kobject_add_internal(struct kobject *kobj)
166{ 160{
167 int error = 0; 161 int error = 0;
168 struct kobject * parent; 162 struct kobject *parent;
169 163
170 if (!(kobj = kobject_get(kobj))) 164 if (!kobj)
171 return -ENOENT; 165 return -ENOENT;
172 if (!kobj->k_name) 166
173 kobject_set_name(kobj, "NO_NAME"); 167 if (!kobj->name || !kobj->name[0]) {
174 if (!*kobj->k_name) { 168 pr_debug("kobject: (%p): attempted to be registered with empty "
175 pr_debug("kobject attempted to be registered with no name!\n"); 169 "name!\n", kobj);
176 WARN_ON(1); 170 WARN_ON(1);
177 kobject_put(kobj);
178 return -EINVAL; 171 return -EINVAL;
179 } 172 }
180 parent = kobject_get(kobj->parent);
181 173
182 pr_debug("kobject %s: registering. parent: %s, set: %s\n", 174 parent = kobject_get(kobj->parent);
183 kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
184 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" );
185 175
176 /* join kset if set, use it as parent if we do not already have one */
186 if (kobj->kset) { 177 if (kobj->kset) {
187 spin_lock(&kobj->kset->list_lock);
188
189 if (!parent) 178 if (!parent)
190 parent = kobject_get(&kobj->kset->kobj); 179 parent = kobject_get(&kobj->kset->kobj);
191 180 kobj_kset_join(kobj);
192 list_add_tail(&kobj->entry,&kobj->kset->list);
193 spin_unlock(&kobj->kset->list_lock);
194 kobj->parent = parent; 181 kobj->parent = parent;
195 } 182 }
196 183
184 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
185 kobject_name(kobj), kobj, __FUNCTION__,
186 parent ? kobject_name(parent) : "<NULL>",
187 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
188
197 error = create_dir(kobj); 189 error = create_dir(kobj);
198 if (error) { 190 if (error) {
199 /* unlink does the kobject_put() for us */ 191 kobj_kset_leave(kobj);
200 unlink(kobj);
201 kobject_put(parent); 192 kobject_put(parent);
193 kobj->parent = NULL;
202 194
203 /* be noisy on error issues */ 195 /* be noisy on error issues */
204 if (error == -EEXIST) 196 if (error == -EEXIST)
205 printk(KERN_ERR "kobject_add failed for %s with " 197 printk(KERN_ERR "%s failed for %s with "
206 "-EEXIST, don't try to register things with " 198 "-EEXIST, don't try to register things with "
207 "the same name in the same directory.\n", 199 "the same name in the same directory.\n",
208 kobject_name(kobj)); 200 __FUNCTION__, kobject_name(kobj));
209 else 201 else
210 printk(KERN_ERR "kobject_add failed for %s (%d)\n", 202 printk(KERN_ERR "%s failed for %s (%d)\n",
211 kobject_name(kobj), error); 203 __FUNCTION__, kobject_name(kobj), error);
212 dump_stack(); 204 dump_stack();
213 } 205 } else
206 kobj->state_in_sysfs = 1;
214 207
215 return error; 208 return error;
216} 209}
217 210
218/** 211/**
219 * kobject_register - initialize and add an object. 212 * kobject_set_name_vargs - Set the name of an kobject
220 * @kobj: object in question. 213 * @kobj: struct kobject to set the name of
214 * @fmt: format string used to build the name
215 * @vargs: vargs to format the string.
221 */ 216 */
222 217static int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
223int kobject_register(struct kobject * kobj) 218 va_list vargs)
224{ 219{
225 int error = -EINVAL; 220 va_list aq;
226 if (kobj) { 221 char *name;
227 kobject_init(kobj); 222
228 error = kobject_add(kobj); 223 va_copy(aq, vargs);
229 if (!error) 224 name = kvasprintf(GFP_KERNEL, fmt, vargs);
230 kobject_uevent(kobj, KOBJ_ADD); 225 va_end(aq);
231 }
232 return error;
233}
234 226
227 if (!name)
228 return -ENOMEM;
229
230 /* Free the old name, if necessary. */
231 kfree(kobj->name);
232
233 /* Now, set the new name */
234 kobj->name = name;
235
236 return 0;
237}
235 238
236/** 239/**
237 * kobject_set_name - Set the name of a kobject 240 * kobject_set_name - Set the name of a kobject
238 * @kobj: kobject to name 241 * @kobj: struct kobject to set the name of
239 * @fmt: format string used to build the name 242 * @fmt: format string used to build the name
240 * 243 *
241 * This sets the name of the kobject. If you have already added the 244 * This sets the name of the kobject. If you have already added the
242 * kobject to the system, you must call kobject_rename() in order to 245 * kobject to the system, you must call kobject_rename() in order to
243 * change the name of the kobject. 246 * change the name of the kobject.
244 */ 247 */
245int kobject_set_name(struct kobject * kobj, const char * fmt, ...) 248int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
246{ 249{
247 int error = 0;
248 int limit;
249 int need;
250 va_list args; 250 va_list args;
251 char *name; 251 int retval;
252 252
253 /* find out how big a buffer we need */
254 name = kmalloc(1024, GFP_KERNEL);
255 if (!name) {
256 error = -ENOMEM;
257 goto done;
258 }
259 va_start(args, fmt); 253 va_start(args, fmt);
260 need = vsnprintf(name, 1024, fmt, args); 254 retval = kobject_set_name_vargs(kobj, fmt, args);
261 va_end(args); 255 va_end(args);
262 kfree(name);
263 256
264 /* Allocate the new space and copy the string in */ 257 return retval;
265 limit = need + 1; 258}
266 name = kmalloc(limit, GFP_KERNEL); 259EXPORT_SYMBOL(kobject_set_name);
267 if (!name) { 260
268 error = -ENOMEM; 261/**
269 goto done; 262 * kobject_init - initialize a kobject structure
263 * @kobj: pointer to the kobject to initialize
264 * @ktype: pointer to the ktype for this kobject.
265 *
266 * This function will properly initialize a kobject such that it can then
267 * be passed to the kobject_add() call.
268 *
269 * After this function is called, the kobject MUST be cleaned up by a call
270 * to kobject_put(), not by a call to kfree directly to ensure that all of
271 * the memory is cleaned up properly.
272 */
273void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
274{
275 char *err_str;
276
277 if (!kobj) {
278 err_str = "invalid kobject pointer!";
279 goto error;
280 }
281 if (!ktype) {
282 err_str = "must have a ktype to be initialized properly!\n";
283 goto error;
284 }
285 if (kobj->state_initialized) {
286 /* do not error out as sometimes we can recover */
287 printk(KERN_ERR "kobject (%p): tried to init an initialized "
288 "object, something is seriously wrong.\n", kobj);
289 dump_stack();
270 } 290 }
271 va_start(args, fmt);
272 need = vsnprintf(name, limit, fmt, args);
273 va_end(args);
274 291
275 /* something wrong with the string we copied? */ 292 kref_init(&kobj->kref);
276 if (need >= limit) { 293 INIT_LIST_HEAD(&kobj->entry);
277 kfree(name); 294 kobj->ktype = ktype;
278 error = -EFAULT; 295 kobj->state_in_sysfs = 0;
279 goto done; 296 kobj->state_add_uevent_sent = 0;
297 kobj->state_remove_uevent_sent = 0;
298 kobj->state_initialized = 1;
299 return;
300
301error:
302 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
303 dump_stack();
304}
305EXPORT_SYMBOL(kobject_init);
306
307static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
308 const char *fmt, va_list vargs)
309{
310 va_list aq;
311 int retval;
312
313 va_copy(aq, vargs);
314 retval = kobject_set_name_vargs(kobj, fmt, aq);
315 va_end(aq);
316 if (retval) {
317 printk(KERN_ERR "kobject: can not set name properly!\n");
318 return retval;
280 } 319 }
320 kobj->parent = parent;
321 return kobject_add_internal(kobj);
322}
281 323
282 /* Free the old name, if necessary. */ 324/**
283 kfree(kobj->k_name); 325 * kobject_add - the main kobject add function
326 * @kobj: the kobject to add
327 * @parent: pointer to the parent of the kobject.
328 * @fmt: format to name the kobject with.
329 *
330 * The kobject name is set and added to the kobject hierarchy in this
331 * function.
332 *
333 * If @parent is set, then the parent of the @kobj will be set to it.
334 * If @parent is NULL, then the parent of the @kobj will be set to the
335 * kobject associted with the kset assigned to this kobject. If no kset
336 * is assigned to the kobject, then the kobject will be located in the
337 * root of the sysfs tree.
338 *
339 * If this function returns an error, kobject_put() must be called to
340 * properly clean up the memory associated with the object.
341 * Under no instance should the kobject that is passed to this function
342 * be directly freed with a call to kfree(), that can leak memory.
343 *
344 * Note, no "add" uevent will be created with this call, the caller should set
345 * up all of the necessary sysfs files for the object and then call
346 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
347 * userspace is properly notified of this kobject's creation.
348 */
349int kobject_add(struct kobject *kobj, struct kobject *parent,
350 const char *fmt, ...)
351{
352 va_list args;
353 int retval;
284 354
285 /* Now, set the new name */ 355 if (!kobj)
286 kobj->k_name = name; 356 return -EINVAL;
287done: 357
288 return error; 358 if (!kobj->state_initialized) {
359 printk(KERN_ERR "kobject '%s' (%p): tried to add an "
360 "uninitialized object, something is seriously wrong.\n",
361 kobject_name(kobj), kobj);
362 dump_stack();
363 return -EINVAL;
364 }
365 va_start(args, fmt);
366 retval = kobject_add_varg(kobj, parent, fmt, args);
367 va_end(args);
368
369 return retval;
289} 370}
290EXPORT_SYMBOL(kobject_set_name); 371EXPORT_SYMBOL(kobject_add);
291 372
292/** 373/**
293 * kobject_rename - change the name of an object 374 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
294 * @kobj: object in question. 375 * @kobj: pointer to the kobject to initialize
295 * @new_name: object's new name 376 * @ktype: pointer to the ktype for this kobject.
377 * @parent: pointer to the parent of this kobject.
378 * @fmt: the name of the kobject.
379 *
380 * This function combines the call to kobject_init() and
381 * kobject_add(). The same type of error handling after a call to
382 * kobject_add() and kobject lifetime rules are the same here.
296 */ 383 */
384int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
385 struct kobject *parent, const char *fmt, ...)
386{
387 va_list args;
388 int retval;
389
390 kobject_init(kobj, ktype);
391
392 va_start(args, fmt);
393 retval = kobject_add_varg(kobj, parent, fmt, args);
394 va_end(args);
395
396 return retval;
397}
398EXPORT_SYMBOL_GPL(kobject_init_and_add);
297 399
298int kobject_rename(struct kobject * kobj, const char *new_name) 400/**
401 * kobject_rename - change the name of an object
402 * @kobj: object in question.
403 * @new_name: object's new name
404 */
405int kobject_rename(struct kobject *kobj, const char *new_name)
299{ 406{
300 int error = 0; 407 int error = 0;
301 const char *devpath = NULL; 408 const char *devpath = NULL;
@@ -334,8 +441,6 @@ int kobject_rename(struct kobject * kobj, const char *new_name)
334 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); 441 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
335 envp[0] = devpath_string; 442 envp[0] = devpath_string;
336 envp[1] = NULL; 443 envp[1] = NULL;
337 /* Note : if we want to send the new name alone, not the full path,
338 * we could probably use kobject_name(kobj); */
339 444
340 error = sysfs_rename_dir(kobj, new_name); 445 error = sysfs_rename_dir(kobj, new_name);
341 446
@@ -354,11 +459,10 @@ out:
354} 459}
355 460
356/** 461/**
357 * kobject_move - move object to another parent 462 * kobject_move - move object to another parent
358 * @kobj: object in question. 463 * @kobj: object in question.
359 * @new_parent: object's new parent (can be NULL) 464 * @new_parent: object's new parent (can be NULL)
360 */ 465 */
361
362int kobject_move(struct kobject *kobj, struct kobject *new_parent) 466int kobject_move(struct kobject *kobj, struct kobject *new_parent)
363{ 467{
364 int error; 468 int error;
@@ -406,68 +510,74 @@ out:
406} 510}
407 511
408/** 512/**
409 * kobject_del - unlink kobject from hierarchy. 513 * kobject_del - unlink kobject from hierarchy.
410 * @kobj: object. 514 * @kobj: object.
411 */ 515 */
412 516void kobject_del(struct kobject *kobj)
413void kobject_del(struct kobject * kobj)
414{ 517{
415 if (!kobj) 518 if (!kobj)
416 return; 519 return;
417 sysfs_remove_dir(kobj);
418 unlink(kobj);
419}
420 520
421/** 521 sysfs_remove_dir(kobj);
422 * kobject_unregister - remove object from hierarchy and decrement refcount. 522 kobj->state_in_sysfs = 0;
423 * @kobj: object going away. 523 kobj_kset_leave(kobj);
424 */ 524 kobject_put(kobj->parent);
425 525 kobj->parent = NULL;
426void kobject_unregister(struct kobject * kobj)
427{
428 if (!kobj)
429 return;
430 pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
431 kobject_uevent(kobj, KOBJ_REMOVE);
432 kobject_del(kobj);
433 kobject_put(kobj);
434} 526}
435 527
436/** 528/**
437 * kobject_get - increment refcount for object. 529 * kobject_get - increment refcount for object.
438 * @kobj: object. 530 * @kobj: object.
439 */ 531 */
440 532struct kobject *kobject_get(struct kobject *kobj)
441struct kobject * kobject_get(struct kobject * kobj)
442{ 533{
443 if (kobj) 534 if (kobj)
444 kref_get(&kobj->kref); 535 kref_get(&kobj->kref);
445 return kobj; 536 return kobj;
446} 537}
447 538
448/** 539/*
449 * kobject_cleanup - free kobject resources. 540 * kobject_cleanup - free kobject resources.
450 * @kobj: object. 541 * @kobj: object to cleanup
451 */ 542 */
452 543static void kobject_cleanup(struct kobject *kobj)
453void kobject_cleanup(struct kobject * kobj)
454{ 544{
455 struct kobj_type * t = get_ktype(kobj); 545 struct kobj_type *t = get_ktype(kobj);
456 struct kset * s = kobj->kset; 546 const char *name = kobj->name;
457 struct kobject * parent = kobj->parent; 547
458 const char *name = kobj->k_name; 548 pr_debug("kobject: '%s' (%p): %s\n",
549 kobject_name(kobj), kobj, __FUNCTION__);
550
551 if (t && !t->release)
552 pr_debug("kobject: '%s' (%p): does not have a release() "
553 "function, it is broken and must be fixed.\n",
554 kobject_name(kobj), kobj);
555
556 /* send "remove" if the caller did not do it but sent "add" */
557 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
558 pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
559 kobject_name(kobj), kobj);
560 kobject_uevent(kobj, KOBJ_REMOVE);
561 }
562
563 /* remove from sysfs if the caller did not do it */
564 if (kobj->state_in_sysfs) {
565 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
566 kobject_name(kobj), kobj);
567 kobject_del(kobj);
568 }
459 569
460 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
461 if (t && t->release) { 570 if (t && t->release) {
571 pr_debug("kobject: '%s' (%p): calling ktype release\n",
572 kobject_name(kobj), kobj);
462 t->release(kobj); 573 t->release(kobj);
463 /* If we have a release function, we can guess that this was 574 }
464 * not a statically allocated kobject, so we should be safe to 575
465 * free the name */ 576 /* free name if we allocated it */
577 if (name) {
578 pr_debug("kobject: '%s': free name\n", name);
466 kfree(name); 579 kfree(name);
467 } 580 }
468 if (s)
469 kset_put(s);
470 kobject_put(parent);
471} 581}
472 582
473static void kobject_release(struct kref *kref) 583static void kobject_release(struct kref *kref)
@@ -476,107 +586,130 @@ static void kobject_release(struct kref *kref)
476} 586}
477 587
478/** 588/**
479 * kobject_put - decrement refcount for object. 589 * kobject_put - decrement refcount for object.
480 * @kobj: object. 590 * @kobj: object.
481 * 591 *
482 * Decrement the refcount, and if 0, call kobject_cleanup(). 592 * Decrement the refcount, and if 0, call kobject_cleanup().
483 */ 593 */
484void kobject_put(struct kobject * kobj) 594void kobject_put(struct kobject *kobj)
485{ 595{
486 if (kobj) 596 if (kobj)
487 kref_put(&kobj->kref, kobject_release); 597 kref_put(&kobj->kref, kobject_release);
488} 598}
489 599
490 600static void dynamic_kobj_release(struct kobject *kobj)
491static void dir_release(struct kobject *kobj)
492{ 601{
602 pr_debug("kobject: (%p): %s\n", kobj, __FUNCTION__);
493 kfree(kobj); 603 kfree(kobj);
494} 604}
495 605
496static struct kobj_type dir_ktype = { 606static struct kobj_type dynamic_kobj_ktype = {
497 .release = dir_release, 607 .release = dynamic_kobj_release,
498 .sysfs_ops = NULL, 608 .sysfs_ops = &kobj_sysfs_ops,
499 .default_attrs = NULL,
500}; 609};
501 610
502/** 611/**
503 * kobject_kset_add_dir - add sub directory of object. 612 * kobject_create - create a struct kobject dynamically
504 * @kset: kset the directory is belongs to.
505 * @parent: object in which a directory is created.
506 * @name: directory name.
507 * 613 *
508 * Add a plain directory object as child of given object. 614 * This function creates a kobject structure dynamically and sets it up
615 * to be a "dynamic" kobject with a default release function set up.
616 *
617 * If the kobject was not able to be created, NULL will be returned.
618 * The kobject structure returned from here must be cleaned up with a
619 * call to kobject_put() and not kfree(), as kobject_init() has
620 * already been called on this structure.
509 */ 621 */
510struct kobject *kobject_kset_add_dir(struct kset *kset, 622struct kobject *kobject_create(void)
511 struct kobject *parent, const char *name)
512{ 623{
513 struct kobject *k; 624 struct kobject *kobj;
514 int ret;
515
516 if (!parent)
517 return NULL;
518
519 k = kzalloc(sizeof(*k), GFP_KERNEL);
520 if (!k)
521 return NULL;
522 625
523 k->kset = kset; 626 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
524 k->parent = parent; 627 if (!kobj)
525 k->ktype = &dir_ktype;
526 kobject_set_name(k, name);
527 ret = kobject_register(k);
528 if (ret < 0) {
529 printk(KERN_WARNING "%s: kobject_register error: %d\n",
530 __func__, ret);
531 kobject_del(k);
532 return NULL; 628 return NULL;
533 }
534 629
535 return k; 630 kobject_init(kobj, &dynamic_kobj_ktype);
631 return kobj;
536} 632}
537 633
538/** 634/**
539 * kobject_add_dir - add sub directory of object. 635 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
540 * @parent: object in which a directory is created. 636 *
541 * @name: directory name. 637 * @name: the name for the kset
638 * @parent: the parent kobject of this kobject, if any.
639 *
640 * This function creates a kset structure dynamically and registers it
641 * with sysfs. When you are finished with this structure, call
642 * kobject_put() and the structure will be dynamically freed when
643 * it is no longer being used.
542 * 644 *
543 * Add a plain directory object as child of given object. 645 * If the kobject was not able to be created, NULL will be returned.
544 */ 646 */
545struct kobject *kobject_add_dir(struct kobject *parent, const char *name) 647struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
546{ 648{
547 return kobject_kset_add_dir(NULL, parent, name); 649 struct kobject *kobj;
650 int retval;
651
652 kobj = kobject_create();
653 if (!kobj)
654 return NULL;
655
656 retval = kobject_add(kobj, parent, "%s", name);
657 if (retval) {
658 printk(KERN_WARNING "%s: kobject_add error: %d\n",
659 __FUNCTION__, retval);
660 kobject_put(kobj);
661 kobj = NULL;
662 }
663 return kobj;
548} 664}
665EXPORT_SYMBOL_GPL(kobject_create_and_add);
549 666
550/** 667/**
551 * kset_init - initialize a kset for use 668 * kset_init - initialize a kset for use
552 * @k: kset 669 * @k: kset
553 */ 670 */
554 671void kset_init(struct kset *k)
555void kset_init(struct kset * k)
556{ 672{
557 kobject_init(&k->kobj); 673 kobject_init_internal(&k->kobj);
558 INIT_LIST_HEAD(&k->list); 674 INIT_LIST_HEAD(&k->list);
559 spin_lock_init(&k->list_lock); 675 spin_lock_init(&k->list_lock);
560} 676}
561 677
678/* default kobject attribute operations */
679static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
680 char *buf)
681{
682 struct kobj_attribute *kattr;
683 ssize_t ret = -EIO;
562 684
563/** 685 kattr = container_of(attr, struct kobj_attribute, attr);
564 * kset_add - add a kset object to the hierarchy. 686 if (kattr->show)
565 * @k: kset. 687 ret = kattr->show(kobj, kattr, buf);
566 */ 688 return ret;
689}
567 690
568int kset_add(struct kset * k) 691static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
692 const char *buf, size_t count)
569{ 693{
570 return kobject_add(&k->kobj); 694 struct kobj_attribute *kattr;
695 ssize_t ret = -EIO;
696
697 kattr = container_of(attr, struct kobj_attribute, attr);
698 if (kattr->store)
699 ret = kattr->store(kobj, kattr, buf, count);
700 return ret;
571} 701}
572 702
703struct sysfs_ops kobj_sysfs_ops = {
704 .show = kobj_attr_show,
705 .store = kobj_attr_store,
706};
573 707
574/** 708/**
575 * kset_register - initialize and add a kset. 709 * kset_register - initialize and add a kset.
576 * @k: kset. 710 * @k: kset.
577 */ 711 */
578 712int kset_register(struct kset *k)
579int kset_register(struct kset * k)
580{ 713{
581 int err; 714 int err;
582 715
@@ -584,46 +717,42 @@ int kset_register(struct kset * k)
584 return -EINVAL; 717 return -EINVAL;
585 718
586 kset_init(k); 719 kset_init(k);
587 err = kset_add(k); 720 err = kobject_add_internal(&k->kobj);
588 if (err) 721 if (err)
589 return err; 722 return err;
590 kobject_uevent(&k->kobj, KOBJ_ADD); 723 kobject_uevent(&k->kobj, KOBJ_ADD);
591 return 0; 724 return 0;
592} 725}
593 726
594
595/** 727/**
596 * kset_unregister - remove a kset. 728 * kset_unregister - remove a kset.
597 * @k: kset. 729 * @k: kset.
598 */ 730 */
599 731void kset_unregister(struct kset *k)
600void kset_unregister(struct kset * k)
601{ 732{
602 if (!k) 733 if (!k)
603 return; 734 return;
604 kobject_unregister(&k->kobj); 735 kobject_put(&k->kobj);
605} 736}
606 737
607
608/** 738/**
609 * kset_find_obj - search for object in kset. 739 * kset_find_obj - search for object in kset.
610 * @kset: kset we're looking in. 740 * @kset: kset we're looking in.
611 * @name: object's name. 741 * @name: object's name.
612 * 742 *
613 * Lock kset via @kset->subsys, and iterate over @kset->list, 743 * Lock kset via @kset->subsys, and iterate over @kset->list,
614 * looking for a matching kobject. If matching object is found 744 * looking for a matching kobject. If matching object is found
615 * take a reference and return the object. 745 * take a reference and return the object.
616 */ 746 */
617 747struct kobject *kset_find_obj(struct kset *kset, const char *name)
618struct kobject * kset_find_obj(struct kset * kset, const char * name)
619{ 748{
620 struct list_head * entry; 749 struct list_head *entry;
621 struct kobject * ret = NULL; 750 struct kobject *ret = NULL;
622 751
623 spin_lock(&kset->list_lock); 752 spin_lock(&kset->list_lock);
624 list_for_each(entry,&kset->list) { 753 list_for_each(entry, &kset->list) {
625 struct kobject * k = to_kobj(entry); 754 struct kobject *k = to_kobj(entry);
626 if (kobject_name(k) && !strcmp(kobject_name(k),name)) { 755 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
627 ret = kobject_get(k); 756 ret = kobject_get(k);
628 break; 757 break;
629 } 758 }
@@ -632,47 +761,94 @@ struct kobject * kset_find_obj(struct kset * kset, const char * name)
632 return ret; 761 return ret;
633} 762}
634 763
635int subsystem_register(struct kset *s) 764static void kset_release(struct kobject *kobj)
636{ 765{
637 return kset_register(s); 766 struct kset *kset = container_of(kobj, struct kset, kobj);
767 pr_debug("kobject: '%s' (%p): %s\n",
768 kobject_name(kobj), kobj, __FUNCTION__);
769 kfree(kset);
638} 770}
639 771
640void subsystem_unregister(struct kset *s) 772static struct kobj_type kset_ktype = {
773 .sysfs_ops = &kobj_sysfs_ops,
774 .release = kset_release,
775};
776
777/**
778 * kset_create - create a struct kset dynamically
779 *
780 * @name: the name for the kset
781 * @uevent_ops: a struct kset_uevent_ops for the kset
782 * @parent_kobj: the parent kobject of this kset, if any.
783 *
784 * This function creates a kset structure dynamically. This structure can
785 * then be registered with the system and show up in sysfs with a call to
786 * kset_register(). When you are finished with this structure, if
787 * kset_register() has been called, call kset_unregister() and the
788 * structure will be dynamically freed when it is no longer being used.
789 *
790 * If the kset was not able to be created, NULL will be returned.
791 */
792static struct kset *kset_create(const char *name,
793 struct kset_uevent_ops *uevent_ops,
794 struct kobject *parent_kobj)
641{ 795{
642 kset_unregister(s); 796 struct kset *kset;
797
798 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
799 if (!kset)
800 return NULL;
801 kobject_set_name(&kset->kobj, name);
802 kset->uevent_ops = uevent_ops;
803 kset->kobj.parent = parent_kobj;
804
805 /*
806 * The kobject of this kset will have a type of kset_ktype and belong to
807 * no kset itself. That way we can properly free it when it is
808 * finished being used.
809 */
810 kset->kobj.ktype = &kset_ktype;
811 kset->kobj.kset = NULL;
812
813 return kset;
643} 814}
644 815
645/** 816/**
646 * subsystem_create_file - export sysfs attribute file. 817 * kset_create_and_add - create a struct kset dynamically and add it to sysfs
647 * @s: subsystem. 818 *
648 * @a: subsystem attribute descriptor. 819 * @name: the name for the kset
820 * @uevent_ops: a struct kset_uevent_ops for the kset
821 * @parent_kobj: the parent kobject of this kset, if any.
822 *
823 * This function creates a kset structure dynamically and registers it
824 * with sysfs. When you are finished with this structure, call
825 * kset_unregister() and the structure will be dynamically freed when it
826 * is no longer being used.
827 *
828 * If the kset was not able to be created, NULL will be returned.
649 */ 829 */
650 830struct kset *kset_create_and_add(const char *name,
651int subsys_create_file(struct kset *s, struct subsys_attribute *a) 831 struct kset_uevent_ops *uevent_ops,
832 struct kobject *parent_kobj)
652{ 833{
653 int error = 0; 834 struct kset *kset;
654 835 int error;
655 if (!s || !a)
656 return -EINVAL;
657 836
658 if (kset_get(s)) { 837 kset = kset_create(name, uevent_ops, parent_kobj);
659 error = sysfs_create_file(&s->kobj, &a->attr); 838 if (!kset)
660 kset_put(s); 839 return NULL;
840 error = kset_register(kset);
841 if (error) {
842 kfree(kset);
843 return NULL;
661 } 844 }
662 return error; 845 return kset;
663} 846}
847EXPORT_SYMBOL_GPL(kset_create_and_add);
664 848
665EXPORT_SYMBOL(kobject_init);
666EXPORT_SYMBOL(kobject_register);
667EXPORT_SYMBOL(kobject_unregister);
668EXPORT_SYMBOL(kobject_get); 849EXPORT_SYMBOL(kobject_get);
669EXPORT_SYMBOL(kobject_put); 850EXPORT_SYMBOL(kobject_put);
670EXPORT_SYMBOL(kobject_add);
671EXPORT_SYMBOL(kobject_del); 851EXPORT_SYMBOL(kobject_del);
672 852
673EXPORT_SYMBOL(kset_register); 853EXPORT_SYMBOL(kset_register);
674EXPORT_SYMBOL(kset_unregister); 854EXPORT_SYMBOL(kset_unregister);
675
676EXPORT_SYMBOL(subsystem_register);
677EXPORT_SYMBOL(subsystem_unregister);
678EXPORT_SYMBOL(subsys_create_file);
diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c
index 5886147252d0..5a402e2982af 100644
--- a/lib/kobject_uevent.c
+++ b/lib/kobject_uevent.c
@@ -98,7 +98,8 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
98 int i = 0; 98 int i = 0;
99 int retval = 0; 99 int retval = 0;
100 100
101 pr_debug("%s\n", __FUNCTION__); 101 pr_debug("kobject: '%s' (%p): %s\n",
102 kobject_name(kobj), kobj, __FUNCTION__);
102 103
103 /* search the kset we belong to */ 104 /* search the kset we belong to */
104 top_kobj = kobj; 105 top_kobj = kobj;
@@ -106,7 +107,9 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
106 top_kobj = top_kobj->parent; 107 top_kobj = top_kobj->parent;
107 108
108 if (!top_kobj->kset) { 109 if (!top_kobj->kset) {
109 pr_debug("kobject attempted to send uevent without kset!\n"); 110 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
111 "without kset!\n", kobject_name(kobj), kobj,
112 __FUNCTION__);
110 return -EINVAL; 113 return -EINVAL;
111 } 114 }
112 115
@@ -116,7 +119,9 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
116 /* skip the event, if the filter returns zero. */ 119 /* skip the event, if the filter returns zero. */
117 if (uevent_ops && uevent_ops->filter) 120 if (uevent_ops && uevent_ops->filter)
118 if (!uevent_ops->filter(kset, kobj)) { 121 if (!uevent_ops->filter(kset, kobj)) {
119 pr_debug("kobject filter function caused the event to drop!\n"); 122 pr_debug("kobject: '%s' (%p): %s: filter function "
123 "caused the event to drop!\n",
124 kobject_name(kobj), kobj, __FUNCTION__);
120 return 0; 125 return 0;
121 } 126 }
122 127
@@ -126,7 +131,9 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
126 else 131 else
127 subsystem = kobject_name(&kset->kobj); 132 subsystem = kobject_name(&kset->kobj);
128 if (!subsystem) { 133 if (!subsystem) {
129 pr_debug("unset subsystem caused the event to drop!\n"); 134 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
135 "event to drop!\n", kobject_name(kobj), kobj,
136 __FUNCTION__);
130 return 0; 137 return 0;
131 } 138 }
132 139
@@ -166,12 +173,24 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
166 if (uevent_ops && uevent_ops->uevent) { 173 if (uevent_ops && uevent_ops->uevent) {
167 retval = uevent_ops->uevent(kset, kobj, env); 174 retval = uevent_ops->uevent(kset, kobj, env);
168 if (retval) { 175 if (retval) {
169 pr_debug ("%s - uevent() returned %d\n", 176 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
170 __FUNCTION__, retval); 177 "%d\n", kobject_name(kobj), kobj,
178 __FUNCTION__, retval);
171 goto exit; 179 goto exit;
172 } 180 }
173 } 181 }
174 182
183 /*
184 * Mark "add" and "remove" events in the object to ensure proper
185 * events to userspace during automatic cleanup. If the object did
186 * send an "add" event, "remove" will automatically generated by
187 * the core, if not already done by the caller.
188 */
189 if (action == KOBJ_ADD)
190 kobj->state_add_uevent_sent = 1;
191 else if (action == KOBJ_REMOVE)
192 kobj->state_remove_uevent_sent = 1;
193
175 /* we will send an event, so request a new sequence number */ 194 /* we will send an event, so request a new sequence number */
176 spin_lock(&sequence_lock); 195 spin_lock(&sequence_lock);
177 seq = ++uevent_seqnum; 196 seq = ++uevent_seqnum;
@@ -219,11 +238,12 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
219 retval = add_uevent_var(env, "HOME=/"); 238 retval = add_uevent_var(env, "HOME=/");
220 if (retval) 239 if (retval)
221 goto exit; 240 goto exit;
222 retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin"); 241 retval = add_uevent_var(env,
242 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
223 if (retval) 243 if (retval)
224 goto exit; 244 goto exit;
225 245
226 call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC); 246 call_usermodehelper(argv[0], argv, env->envp, UMH_WAIT_EXEC);
227 } 247 }
228 248
229exit: 249exit:
@@ -231,7 +251,6 @@ exit:
231 kfree(env); 251 kfree(env);
232 return retval; 252 return retval;
233} 253}
234
235EXPORT_SYMBOL_GPL(kobject_uevent_env); 254EXPORT_SYMBOL_GPL(kobject_uevent_env);
236 255
237/** 256/**
@@ -247,7 +266,6 @@ int kobject_uevent(struct kobject *kobj, enum kobject_action action)
247{ 266{
248 return kobject_uevent_env(kobj, action, NULL); 267 return kobject_uevent_env(kobj, action, NULL);
249} 268}
250
251EXPORT_SYMBOL_GPL(kobject_uevent); 269EXPORT_SYMBOL_GPL(kobject_uevent);
252 270
253/** 271/**
diff --git a/lib/kref.c b/lib/kref.c
index a6dc3ec328e0..9ecd6e865610 100644
--- a/lib/kref.c
+++ b/lib/kref.c
@@ -15,13 +15,23 @@
15#include <linux/module.h> 15#include <linux/module.h>
16 16
17/** 17/**
18 * kref_set - initialize object and set refcount to requested number.
19 * @kref: object in question.
20 * @num: initial reference counter
21 */
22void kref_set(struct kref *kref, int num)
23{
24 atomic_set(&kref->refcount, num);
25 smp_mb();
26}
27
28/**
18 * kref_init - initialize object. 29 * kref_init - initialize object.
19 * @kref: object in question. 30 * @kref: object in question.
20 */ 31 */
21void kref_init(struct kref *kref) 32void kref_init(struct kref *kref)
22{ 33{
23 atomic_set(&kref->refcount,1); 34 kref_set(kref, 1);
24 smp_mb();
25} 35}
26 36
27/** 37/**
@@ -61,6 +71,7 @@ int kref_put(struct kref *kref, void (*release)(struct kref *kref))
61 return 0; 71 return 0;
62} 72}
63 73
74EXPORT_SYMBOL(kref_set);
64EXPORT_SYMBOL(kref_init); 75EXPORT_SYMBOL(kref_init);
65EXPORT_SYMBOL(kref_get); 76EXPORT_SYMBOL(kref_get);
66EXPORT_SYMBOL(kref_put); 77EXPORT_SYMBOL(kref_put);
diff --git a/mm/slub.c b/mm/slub.c
index 474945ecd89d..5cc4b7dddb50 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -3962,7 +3962,7 @@ static struct kset_uevent_ops slab_uevent_ops = {
3962 .filter = uevent_filter, 3962 .filter = uevent_filter,
3963}; 3963};
3964 3964
3965static decl_subsys(slab, &slab_ktype, &slab_uevent_ops); 3965static struct kset *slab_kset;
3966 3966
3967#define ID_STR_LENGTH 64 3967#define ID_STR_LENGTH 64
3968 3968
@@ -4015,7 +4015,7 @@ static int sysfs_slab_add(struct kmem_cache *s)
4015 * This is typically the case for debug situations. In that 4015 * This is typically the case for debug situations. In that
4016 * case we can catch duplicate names easily. 4016 * case we can catch duplicate names easily.
4017 */ 4017 */
4018 sysfs_remove_link(&slab_subsys.kobj, s->name); 4018 sysfs_remove_link(&slab_kset->kobj, s->name);
4019 name = s->name; 4019 name = s->name;
4020 } else { 4020 } else {
4021 /* 4021 /*
@@ -4025,12 +4025,12 @@ static int sysfs_slab_add(struct kmem_cache *s)
4025 name = create_unique_id(s); 4025 name = create_unique_id(s);
4026 } 4026 }
4027 4027
4028 kobj_set_kset_s(s, slab_subsys); 4028 s->kobj.kset = slab_kset;
4029 kobject_set_name(&s->kobj, name); 4029 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4030 kobject_init(&s->kobj); 4030 if (err) {
4031 err = kobject_add(&s->kobj); 4031 kobject_put(&s->kobj);
4032 if (err)
4033 return err; 4032 return err;
4033 }
4034 4034
4035 err = sysfs_create_group(&s->kobj, &slab_attr_group); 4035 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4036 if (err) 4036 if (err)
@@ -4070,9 +4070,8 @@ static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4070 /* 4070 /*
4071 * If we have a leftover link then remove it. 4071 * If we have a leftover link then remove it.
4072 */ 4072 */
4073 sysfs_remove_link(&slab_subsys.kobj, name); 4073 sysfs_remove_link(&slab_kset->kobj, name);
4074 return sysfs_create_link(&slab_subsys.kobj, 4074 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
4075 &s->kobj, name);
4076 } 4075 }
4077 4076
4078 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL); 4077 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
@@ -4091,8 +4090,8 @@ static int __init slab_sysfs_init(void)
4091 struct kmem_cache *s; 4090 struct kmem_cache *s;
4092 int err; 4091 int err;
4093 4092
4094 err = subsystem_register(&slab_subsys); 4093 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
4095 if (err) { 4094 if (!slab_kset) {
4096 printk(KERN_ERR "Cannot register slab subsystem.\n"); 4095 printk(KERN_ERR "Cannot register slab subsystem.\n");
4097 return -ENOSYS; 4096 return -ENOSYS;
4098 } 4097 }
diff --git a/net/bridge/br_if.c b/net/bridge/br_if.c
index 935784f736b3..298e0f463c56 100644
--- a/net/bridge/br_if.c
+++ b/net/bridge/br_if.c
@@ -133,7 +133,7 @@ static void del_nbp(struct net_bridge_port *p)
133 struct net_bridge *br = p->br; 133 struct net_bridge *br = p->br;
134 struct net_device *dev = p->dev; 134 struct net_device *dev = p->dev;
135 135
136 sysfs_remove_link(&br->ifobj, dev->name); 136 sysfs_remove_link(br->ifobj, dev->name);
137 137
138 dev_set_promiscuity(dev, -1); 138 dev_set_promiscuity(dev, -1);
139 139
@@ -258,12 +258,6 @@ static struct net_bridge_port *new_nbp(struct net_bridge *br,
258 p->state = BR_STATE_DISABLED; 258 p->state = BR_STATE_DISABLED;
259 br_stp_port_timer_init(p); 259 br_stp_port_timer_init(p);
260 260
261 kobject_init(&p->kobj);
262 kobject_set_name(&p->kobj, SYSFS_BRIDGE_PORT_ATTR);
263 p->kobj.ktype = &brport_ktype;
264 p->kobj.parent = &(dev->dev.kobj);
265 p->kobj.kset = NULL;
266
267 return p; 261 return p;
268} 262}
269 263
@@ -379,7 +373,8 @@ int br_add_if(struct net_bridge *br, struct net_device *dev)
379 if (IS_ERR(p)) 373 if (IS_ERR(p))
380 return PTR_ERR(p); 374 return PTR_ERR(p);
381 375
382 err = kobject_add(&p->kobj); 376 err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
377 SYSFS_BRIDGE_PORT_ATTR);
383 if (err) 378 if (err)
384 goto err0; 379 goto err0;
385 380
@@ -416,6 +411,7 @@ err2:
416 br_fdb_delete_by_port(br, p, 1); 411 br_fdb_delete_by_port(br, p, 1);
417err1: 412err1:
418 kobject_del(&p->kobj); 413 kobject_del(&p->kobj);
414 return err;
419err0: 415err0:
420 kobject_put(&p->kobj); 416 kobject_put(&p->kobj);
421 return err; 417 return err;
diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
index f666f7b28ff5..c11b554fd109 100644
--- a/net/bridge/br_private.h
+++ b/net/bridge/br_private.h
@@ -124,7 +124,7 @@ struct net_bridge
124 struct timer_list tcn_timer; 124 struct timer_list tcn_timer;
125 struct timer_list topology_change_timer; 125 struct timer_list topology_change_timer;
126 struct timer_list gc_timer; 126 struct timer_list gc_timer;
127 struct kobject ifobj; 127 struct kobject *ifobj;
128}; 128};
129 129
130extern struct notifier_block br_device_notifier; 130extern struct notifier_block br_device_notifier;
diff --git a/net/bridge/br_sysfs_br.c b/net/bridge/br_sysfs_br.c
index 3312e8f2abe4..9cf0538d1717 100644
--- a/net/bridge/br_sysfs_br.c
+++ b/net/bridge/br_sysfs_br.c
@@ -426,16 +426,10 @@ int br_sysfs_addbr(struct net_device *dev)
426 goto out2; 426 goto out2;
427 } 427 }
428 428
429 429 br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
430 kobject_set_name(&br->ifobj, SYSFS_BRIDGE_PORT_SUBDIR); 430 if (!br->ifobj) {
431 br->ifobj.ktype = NULL;
432 br->ifobj.kset = NULL;
433 br->ifobj.parent = brobj;
434
435 err = kobject_register(&br->ifobj);
436 if (err) {
437 pr_info("%s: can't add kobject (directory) %s/%s\n", 431 pr_info("%s: can't add kobject (directory) %s/%s\n",
438 __FUNCTION__, dev->name, kobject_name(&br->ifobj)); 432 __FUNCTION__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
439 goto out3; 433 goto out3;
440 } 434 }
441 return 0; 435 return 0;
@@ -453,7 +447,7 @@ void br_sysfs_delbr(struct net_device *dev)
453 struct kobject *kobj = &dev->dev.kobj; 447 struct kobject *kobj = &dev->dev.kobj;
454 struct net_bridge *br = netdev_priv(dev); 448 struct net_bridge *br = netdev_priv(dev);
455 449
456 kobject_unregister(&br->ifobj); 450 kobject_put(br->ifobj);
457 sysfs_remove_bin_file(kobj, &bridge_forward); 451 sysfs_remove_bin_file(kobj, &bridge_forward);
458 sysfs_remove_group(kobj, &bridge_group); 452 sysfs_remove_group(kobj, &bridge_group);
459} 453}
diff --git a/net/bridge/br_sysfs_if.c b/net/bridge/br_sysfs_if.c
index 79db51fcb476..02b2d50cce4d 100644
--- a/net/bridge/br_sysfs_if.c
+++ b/net/bridge/br_sysfs_if.c
@@ -229,7 +229,7 @@ int br_sysfs_addif(struct net_bridge_port *p)
229 goto out2; 229 goto out2;
230 } 230 }
231 231
232 err= sysfs_create_link(&br->ifobj, &p->kobj, p->dev->name); 232 err = sysfs_create_link(br->ifobj, &p->kobj, p->dev->name);
233out2: 233out2:
234 return err; 234 return err;
235} 235}
diff --git a/samples/Kconfig b/samples/Kconfig
index 57bb2236952c..74d97cc24787 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -13,4 +13,14 @@ config SAMPLE_MARKERS
13 help 13 help
14 This build markers example modules. 14 This build markers example modules.
15 15
16config SAMPLE_KOBJECT
17 tristate "Build kobject examples"
18 help
19 This config option will allow you to build a number of
20 different kobject sample modules showing how to use kobjects,
21 ksets, and ktypes properly.
22
23 If in doubt, say "N" here.
24
16endif # SAMPLES 25endif # SAMPLES
26
diff --git a/samples/Makefile b/samples/Makefile
index 5a4f0b6bcbed..8652d0f268ad 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,3 +1,3 @@
1# Makefile for Linux samples code 1# Makefile for Linux samples code
2 2
3obj-$(CONFIG_SAMPLES) += markers/ 3obj-$(CONFIG_SAMPLES) += markers/ kobject/
diff --git a/samples/kobject/Makefile b/samples/kobject/Makefile
new file mode 100644
index 000000000000..4a194203c982
--- /dev/null
+++ b/samples/kobject/Makefile
@@ -0,0 +1 @@
obj-$(CONFIG_SAMPLE_KOBJECT) += kobject-example.o kset-example.o
diff --git a/samples/kobject/kobject-example.c b/samples/kobject/kobject-example.c
new file mode 100644
index 000000000000..08d0d3ff3263
--- /dev/null
+++ b/samples/kobject/kobject-example.c
@@ -0,0 +1,137 @@
1/*
2 * Sample kobject implementation
3 *
4 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2007 Novell Inc.
6 *
7 * Released under the GPL version 2 only.
8 *
9 */
10#include <linux/kobject.h>
11#include <linux/string.h>
12#include <linux/sysfs.h>
13#include <linux/module.h>
14#include <linux/init.h>
15
16/*
17 * This module shows how to create a simple subdirectory in sysfs called
18 * /sys/kernel/kobject-example In that directory, 3 files are created:
19 * "foo", "baz", and "bar". If an integer is written to these files, it can be
20 * later read out of it.
21 */
22
23static int foo;
24static int baz;
25static int bar;
26
27/*
28 * The "foo" file where a static variable is read from and written to.
29 */
30static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
31 char *buf)
32{
33 return sprintf(buf, "%d\n", foo);
34}
35
36static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
37 const char *buf, size_t count)
38{
39 sscanf(buf, "%du", &foo);
40 return count;
41}
42
43static struct kobj_attribute foo_attribute =
44 __ATTR(foo, 0666, foo_show, foo_store);
45
46/*
47 * More complex function where we determine which varible is being accessed by
48 * looking at the attribute for the "baz" and "bar" files.
49 */
50static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
51 char *buf)
52{
53 int var;
54
55 if (strcmp(attr->attr.name, "baz") == 0)
56 var = baz;
57 else
58 var = bar;
59 return sprintf(buf, "%d\n", var);
60}
61
62static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
63 const char *buf, size_t count)
64{
65 int var;
66
67 sscanf(buf, "%du", &var);
68 if (strcmp(attr->attr.name, "baz") == 0)
69 baz = var;
70 else
71 bar = var;
72 return count;
73}
74
75static struct kobj_attribute baz_attribute =
76 __ATTR(baz, 0666, b_show, b_store);
77static struct kobj_attribute bar_attribute =
78 __ATTR(bar, 0666, b_show, b_store);
79
80
81/*
82 * Create a group of attributes so that we can create and destory them all
83 * at once.
84 */
85static struct attribute *attrs[] = {
86 &foo_attribute.attr,
87 &baz_attribute.attr,
88 &bar_attribute.attr,
89 NULL, /* need to NULL terminate the list of attributes */
90};
91
92/*
93 * An unnamed attribute group will put all of the attributes directly in
94 * the kobject directory. If we specify a name, a subdirectory will be
95 * created for the attributes with the directory being the name of the
96 * attribute group.
97 */
98static struct attribute_group attr_group = {
99 .attrs = attrs,
100};
101
102static struct kobject *example_kobj;
103
104static int example_init(void)
105{
106 int retval;
107
108 /*
109 * Create a simple kobject with the name of "kobject_example",
110 * located under /sys/kernel/
111 *
112 * As this is a simple directory, no uevent will be sent to
113 * userspace. That is why this function should not be used for
114 * any type of dynamic kobjects, where the name and number are
115 * not known ahead of time.
116 */
117 example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
118 if (!example_kobj)
119 return -ENOMEM;
120
121 /* Create the files associated with this kobject */
122 retval = sysfs_create_group(example_kobj, &attr_group);
123 if (retval)
124 kobject_put(example_kobj);
125
126 return retval;
127}
128
129static void example_exit(void)
130{
131 kobject_put(example_kobj);
132}
133
134module_init(example_init);
135module_exit(example_exit);
136MODULE_LICENSE("GPL");
137MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
diff --git a/samples/kobject/kset-example.c b/samples/kobject/kset-example.c
new file mode 100644
index 000000000000..b0a1b4fe6584
--- /dev/null
+++ b/samples/kobject/kset-example.c
@@ -0,0 +1,278 @@
1/*
2 * Sample kset and ktype implementation
3 *
4 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2007 Novell Inc.
6 *
7 * Released under the GPL version 2 only.
8 *
9 */
10#include <linux/kobject.h>
11#include <linux/string.h>
12#include <linux/sysfs.h>
13#include <linux/module.h>
14#include <linux/init.h>
15
16/*
17 * This module shows how to create a kset in sysfs called
18 * /sys/kernel/kset-example
19 * Then tree kobjects are created and assigned to this kset, "foo", "baz",
20 * and "bar". In those kobjects, attributes of the same name are also
21 * created and if an integer is written to these files, it can be later
22 * read out of it.
23 */
24
25
26/*
27 * This is our "object" that we will create a few of and register them with
28 * sysfs.
29 */
30struct foo_obj {
31 struct kobject kobj;
32 int foo;
33 int baz;
34 int bar;
35};
36#define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
37
38/* a custom attribute that works just for a struct foo_obj. */
39struct foo_attribute {
40 struct attribute attr;
41 ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
42 ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
43};
44#define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
45
46/*
47 * The default show function that must be passed to sysfs. This will be
48 * called by sysfs for whenever a show function is called by the user on a
49 * sysfs file associated with the kobjects we have registered. We need to
50 * transpose back from a "default" kobject to our custom struct foo_obj and
51 * then call the show function for that specific object.
52 */
53static ssize_t foo_attr_show(struct kobject *kobj,
54 struct attribute *attr,
55 char *buf)
56{
57 struct foo_attribute *attribute;
58 struct foo_obj *foo;
59
60 attribute = to_foo_attr(attr);
61 foo = to_foo_obj(kobj);
62
63 if (!attribute->show)
64 return -EIO;
65
66 return attribute->show(foo, attribute, buf);
67}
68
69/*
70 * Just like the default show function above, but this one is for when the
71 * sysfs "store" is requested (when a value is written to a file.)
72 */
73static ssize_t foo_attr_store(struct kobject *kobj,
74 struct attribute *attr,
75 const char *buf, size_t len)
76{
77 struct foo_attribute *attribute;
78 struct foo_obj *foo;
79
80 attribute = to_foo_attr(attr);
81 foo = to_foo_obj(kobj);
82
83 if (!attribute->store)
84 return -EIO;
85
86 return attribute->store(foo, attribute, buf, len);
87}
88
89/* Our custom sysfs_ops that we will associate with our ktype later on */
90static struct sysfs_ops foo_sysfs_ops = {
91 .show = foo_attr_show,
92 .store = foo_attr_store,
93};
94
95/*
96 * The release function for our object. This is REQUIRED by the kernel to
97 * have. We free the memory held in our object here.
98 *
99 * NEVER try to get away with just a "blank" release function to try to be
100 * smarter than the kernel. Turns out, no one ever is...
101 */
102static void foo_release(struct kobject *kobj)
103{
104 struct foo_obj *foo;
105
106 foo = to_foo_obj(kobj);
107 kfree(foo);
108}
109
110/*
111 * The "foo" file where the .foo variable is read from and written to.
112 */
113static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
114 char *buf)
115{
116 return sprintf(buf, "%d\n", foo_obj->foo);
117}
118
119static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
120 const char *buf, size_t count)
121{
122 sscanf(buf, "%du", &foo_obj->foo);
123 return count;
124}
125
126static struct foo_attribute foo_attribute =
127 __ATTR(foo, 0666, foo_show, foo_store);
128
129/*
130 * More complex function where we determine which varible is being accessed by
131 * looking at the attribute for the "baz" and "bar" files.
132 */
133static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
134 char *buf)
135{
136 int var;
137
138 if (strcmp(attr->attr.name, "baz") == 0)
139 var = foo_obj->baz;
140 else
141 var = foo_obj->bar;
142 return sprintf(buf, "%d\n", var);
143}
144
145static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
146 const char *buf, size_t count)
147{
148 int var;
149
150 sscanf(buf, "%du", &var);
151 if (strcmp(attr->attr.name, "baz") == 0)
152 foo_obj->baz = var;
153 else
154 foo_obj->bar = var;
155 return count;
156}
157
158static struct foo_attribute baz_attribute =
159 __ATTR(baz, 0666, b_show, b_store);
160static struct foo_attribute bar_attribute =
161 __ATTR(bar, 0666, b_show, b_store);
162
163/*
164 * Create a group of attributes so that we can create and destory them all
165 * at once.
166 */
167static struct attribute *foo_default_attrs[] = {
168 &foo_attribute.attr,
169 &baz_attribute.attr,
170 &bar_attribute.attr,
171 NULL, /* need to NULL terminate the list of attributes */
172};
173
174/*
175 * Our own ktype for our kobjects. Here we specify our sysfs ops, the
176 * release function, and the set of default attributes we want created
177 * whenever a kobject of this type is registered with the kernel.
178 */
179static struct kobj_type foo_ktype = {
180 .sysfs_ops = &foo_sysfs_ops,
181 .release = foo_release,
182 .default_attrs = foo_default_attrs,
183};
184
185static struct kset *example_kset;
186static struct foo_obj *foo_obj;
187static struct foo_obj *bar_obj;
188static struct foo_obj *baz_obj;
189
190static struct foo_obj *create_foo_obj(const char *name)
191{
192 struct foo_obj *foo;
193 int retval;
194
195 /* allocate the memory for the whole object */
196 foo = kzalloc(sizeof(*foo), GFP_KERNEL);
197 if (!foo)
198 return NULL;
199
200 /*
201 * As we have a kset for this kobject, we need to set it before calling
202 * the kobject core.
203 */
204 foo->kobj.kset = example_kset;
205
206 /*
207 * Initialize and add the kobject to the kernel. All the default files
208 * will be created here. As we have already specified a kset for this
209 * kobject, we don't have to set a parent for the kobject, the kobject
210 * will be placed beneath that kset automatically.
211 */
212 retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
213 if (retval) {
214 kfree(foo);
215 return NULL;
216 }
217
218 /*
219 * We are always responsible for sending the uevent that the kobject
220 * was added to the system.
221 */
222 kobject_uevent(&foo->kobj, KOBJ_ADD);
223
224 return foo;
225}
226
227static void destroy_foo_obj(struct foo_obj *foo)
228{
229 kobject_put(&foo->kobj);
230}
231
232static int example_init(void)
233{
234 /*
235 * Create a kset with the name of "kset_example",
236 * located under /sys/kernel/
237 */
238 example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
239 if (!example_kset)
240 return -ENOMEM;
241
242 /*
243 * Create three objects and register them with our kset
244 */
245 foo_obj = create_foo_obj("foo");
246 if (!foo_obj)
247 goto foo_error;
248
249 bar_obj = create_foo_obj("bar");
250 if (!bar_obj)
251 goto bar_error;
252
253 baz_obj = create_foo_obj("baz");
254 if (!baz_obj)
255 goto baz_error;
256
257 return 0;
258
259baz_error:
260 destroy_foo_obj(bar_obj);
261bar_error:
262 destroy_foo_obj(foo_obj);
263foo_error:
264 return -EINVAL;
265}
266
267static void example_exit(void)
268{
269 destroy_foo_obj(baz_obj);
270 destroy_foo_obj(bar_obj);
271 destroy_foo_obj(foo_obj);
272 kset_unregister(example_kset);
273}
274
275module_init(example_init);
276module_exit(example_exit);
277MODULE_LICENSE("GPL");
278MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
diff --git a/security/inode.c b/security/inode.c
index b28a8acae34d..acc6cf0d7900 100644
--- a/security/inode.c
+++ b/security/inode.c
@@ -315,20 +315,19 @@ void securityfs_remove(struct dentry *dentry)
315} 315}
316EXPORT_SYMBOL_GPL(securityfs_remove); 316EXPORT_SYMBOL_GPL(securityfs_remove);
317 317
318static decl_subsys(security, NULL, NULL); 318static struct kobject *security_kobj;
319 319
320static int __init securityfs_init(void) 320static int __init securityfs_init(void)
321{ 321{
322 int retval; 322 int retval;
323 323
324 kobj_set_kset_s(&security_subsys, kernel_subsys); 324 security_kobj = kobject_create_and_add("security", kernel_kobj);
325 retval = subsystem_register(&security_subsys); 325 if (!security_kobj)
326 if (retval) 326 return -EINVAL;
327 return retval;
328 327
329 retval = register_filesystem(&fs_type); 328 retval = register_filesystem(&fs_type);
330 if (retval) 329 if (retval)
331 subsystem_unregister(&security_subsys); 330 kobject_put(security_kobj);
332 return retval; 331 return retval;
333} 332}
334 333