Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-2.6

This can be broken down into these major areas:
 - Documentation updates (language translations and fixes, as
   well as kobject and kset documenatation updates.)
 - major kset/kobject/ktype rework and fixes.  This cleans up the
   kset and kobject and ktype relationship and architecture,
   making sense of things now, and good documenation and samples
   are provided for others to use.  Also the attributes for
   kobjects are much easier to handle now.  This cleaned up a LOT
   of code all through the kernel, making kobjects easier to use
   if you want to.
 - struct bus_type has been reworked to now handle the lifetime
   rules properly, as the kobject is properly dynamic.
 - struct driver has also been reworked, and now the lifetime
   issues are resolved.
 - the block subsystem has been converted to use struct device
   now, and not "raw" kobjects.  This patch has been in the -mm
   tree for over a year now, and finally all the issues are
   worked out with it.  Older distros now properly work with new
   kernels, and no userspace updates are needed at all.
 - nozomi driver is added.  This has also been in -mm for a long
   time, and many people have asked for it to go in.  It is now
   in good enough shape to do so.
 - lots of class_device conversions to use struct device instead.
   The tree is almost all cleaned up now, only SCSI and IB is the
   remaining code to fix up...

* git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-2.6: (196 commits)
  Driver core: coding style fixes
  Kobject: fix coding style issues in kobject c files
  Kobject: fix coding style issues in kobject.h
  Driver core: fix coding style issues in device.h
  spi: use class iteration api
  scsi: use class iteration api
  rtc: use class iteration api
  power supply : use class iteration api
  ieee1394: use class iteration api
  Driver Core: add class iteration api
  Driver core: Cleanup get_device_parent() in device_add() and device_move()
  UIO: constify function pointer tables
  Driver Core: constify the name passed to platform_device_register_simple
  driver core: fix build with SYSFS=n
  sysfs: make SYSFS_DEPRECATED depend on SYSFS
  Driver core: use LIST_HEAD instead of call to INIT_LIST_HEAD in __init
  kobject: add sample code for how to use ksets/ktypes/kobjects
  kobject: add sample code for how to use kobjects in a simple manner.
  kobject: update the kobject/kset documentation
  kobject: remove old, outdated documentation.
  ...
This commit is contained in:
Linus Torvalds 2008-01-25 08:34:42 -08:00
commit df8dc74e8a
217 changed files with 8960 additions and 4721 deletions

View file

@ -1,289 +1,386 @@
The kobject Infrastructure
Everything you never wanted to know about kobjects, ksets, and ktypes
Patrick Mochel <mochel@osdl.org>
Greg Kroah-Hartman <gregkh@suse.de>
Updated: 3 June 2003
Based on an original article by Jon Corbet for lwn.net written October 1,
2003 and located at http://lwn.net/Articles/51437/
Last updated December 19, 2007
Copyright (c) 2003 Patrick Mochel
Copyright (c) 2003 Open Source Development Labs
Part of the difficulty in understanding the driver model - and the kobject
abstraction upon which it is built - is that there is no obvious starting
place. Dealing with kobjects requires understanding a few different types,
all of which make reference to each other. In an attempt to make things
easier, we'll take a multi-pass approach, starting with vague terms and
adding detail as we go. To that end, here are some quick definitions of
some terms we will be working with.
- A kobject is an object of type struct kobject. Kobjects have a name
and a reference count. A kobject also has a parent pointer (allowing
objects to be arranged into hierarchies), a specific type, and,
usually, a representation in the sysfs virtual filesystem.
Kobjects are generally not interesting on their own; instead, they are
usually embedded within some other structure which contains the stuff
the code is really interested in.
No structure should EVER have more than one kobject embedded within it.
If it does, the reference counting for the object is sure to be messed
up and incorrect, and your code will be buggy. So do not do this.
- A ktype is the type of object that embeds a kobject. Every structure
that embeds a kobject needs a corresponding ktype. The ktype controls
what happens to the kobject when it is created and destroyed.
- A kset is a group of kobjects. These kobjects can be of the same ktype
or belong to different ktypes. The kset is the basic container type for
collections of kobjects. Ksets contain their own kobjects, but you can
safely ignore that implementation detail as the kset core code handles
this kobject automatically.
When you see a sysfs directory full of other directories, generally each
of those directories corresponds to a kobject in the same kset.
We'll look at how to create and manipulate all of these types. A bottom-up
approach will be taken, so we'll go back to kobjects.
0. Introduction
Embedding kobjects
The kobject infrastructure performs basic object management that larger
data structures and subsystems can leverage, rather than reimplement
similar functionality. This functionality primarily concerns:
It is rare for kernel code to create a standalone kobject, with one major
exception explained below. Instead, kobjects are used to control access to
a larger, domain-specific object. To this end, kobjects will be found
embedded in other structures. If you are used to thinking of things in
object-oriented terms, kobjects can be seen as a top-level, abstract class
from which other classes are derived. A kobject implements a set of
capabilities which are not particularly useful by themselves, but which are
nice to have in other objects. The C language does not allow for the
direct expression of inheritance, so other techniques - such as structure
embedding - must be used.
- Object reference counting.
- Maintaining lists (sets) of objects.
- Object set locking.
- Userspace representation.
So, for example, the UIO code has a structure that defines the memory
region associated with a uio device:
The infrastructure consists of a number of object types to support
this functionality. Their programming interfaces are described below
in detail, and briefly here:
- kobjects a simple object.
- kset a set of objects of a certain type.
- ktype a set of helpers for objects of a common type.
The kobject infrastructure maintains a close relationship with the
sysfs filesystem. Each kobject that is registered with the kobject
core receives a directory in sysfs. Attributes about the kobject can
then be exported. Please see Documentation/filesystems/sysfs.txt for
more information.
The kobject infrastructure provides a flexible programming interface,
and allows kobjects and ksets to be used without being registered
(i.e. with no sysfs representation). This is also described later.
1. kobjects
1.1 Description
struct kobject is a simple data type that provides a foundation for
more complex object types. It provides a set of basic fields that
almost all complex data types share. kobjects are intended to be
embedded in larger data structures and replace fields they duplicate.
1.2 Definition
struct kobject {
const char * k_name;
struct kref kref;
struct list_head entry;
struct kobject * parent;
struct kset * kset;
struct kobj_type * ktype;
struct sysfs_dirent * sd;
wait_queue_head_t poll;
struct uio_mem {
struct kobject kobj;
unsigned long addr;
unsigned long size;
int memtype;
void __iomem *internal_addr;
};
void kobject_init(struct kobject *);
int kobject_add(struct kobject *);
int kobject_register(struct kobject *);
If you have a struct uio_mem structure, finding its embedded kobject is
just a matter of using the kobj member. Code that works with kobjects will
often have the opposite problem, however: given a struct kobject pointer,
what is the pointer to the containing structure? You must avoid tricks
(such as assuming that the kobject is at the beginning of the structure)
and, instead, use the container_of() macro, found in <linux/kernel.h>:
void kobject_del(struct kobject *);
void kobject_unregister(struct kobject *);
container_of(pointer, type, member)
struct kobject * kobject_get(struct kobject *);
void kobject_put(struct kobject *);
where pointer is the pointer to the embedded kobject, type is the type of
the containing structure, and member is the name of the structure field to
which pointer points. The return value from container_of() is a pointer to
the given type. So, for example, a pointer "kp" to a struct kobject
embedded within a struct uio_mem could be converted to a pointer to the
containing uio_mem structure with:
struct uio_mem *u_mem = container_of(kp, struct uio_mem, kobj);
Programmers often define a simple macro for "back-casting" kobject pointers
to the containing type.
1.3 kobject Programming Interface
Initialization of kobjects
kobjects may be dynamically added and removed from the kobject core
using kobject_register() and kobject_unregister(). Registration
includes inserting the kobject in the list of its dominant kset and
creating a directory for it in sysfs.
Code which creates a kobject must, of course, initialize that object. Some
of the internal fields are setup with a (mandatory) call to kobject_init():
Alternatively, one may use a kobject without adding it to its kset's list
or exporting it via sysfs, by simply calling kobject_init(). An
initialized kobject may later be added to the object hierarchy by
calling kobject_add(). An initialized kobject may be used for
reference counting.
void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
Note: calling kobject_init() then kobject_add() is functionally
equivalent to calling kobject_register().
The ktype is required for a kobject to be created properly, as every kobject
must have an associated kobj_type. After calling kobject_init(), to
register the kobject with sysfs, the function kobject_add() must be called:
When a kobject is unregistered, it is removed from its kset's list,
removed from the sysfs filesystem, and its reference count is decremented.
List and sysfs removal happen in kobject_del(), and may be called
manually. kobject_put() decrements the reference count, and may also
be called manually.
int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...);
A kobject's reference count may be incremented with kobject_get(),
which returns a valid reference to a kobject; and decremented with
kobject_put(). An object's reference count may only be incremented if
it is already positive.
This sets up the parent of the kobject and the name for the kobject
properly. If the kobject is to be associated with a specific kset,
kobj->kset must be assigned before calling kobject_add(). If a kset is
associated with a kobject, then the parent for the kobject can be set to
NULL in the call to kobject_add() and then the kobject's parent will be the
kset itself.
When a kobject's reference count reaches 0, the method struct
kobj_type::release() (which the kobject's kset points to) is called.
This allows any memory allocated for the object to be freed.
As the name of the kobject is set when it is added to the kernel, the name
of the kobject should never be manipulated directly. If you must change
the name of the kobject, call kobject_rename():
int kobject_rename(struct kobject *kobj, const char *new_name);
There is a function called kobject_set_name() but that is legacy cruft and
is being removed. If your code needs to call this function, it is
incorrect and needs to be fixed.
To properly access the name of the kobject, use the function
kobject_name():
const char *kobject_name(const struct kobject * kobj);
There is a helper function to both initialize and add the kobject to the
kernel at the same time, called supprisingly enough kobject_init_and_add():
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
struct kobject *parent, const char *fmt, ...);
The arguments are the same as the individual kobject_init() and
kobject_add() functions described above.
NOTE!!!
Uevents
It is _imperative_ that you supply a destructor for dynamically
allocated kobjects to free them if you are using kobject reference
counts. The reference count controls the lifetime of the object.
If it goes to 0, then it is assumed that the object will
be freed and cannot be used.
After a kobject has been registered with the kobject core, you need to
announce to the world that it has been created. This can be done with a
call to kobject_uevent():
More importantly, you must free the object there, and not immediately
after an unregister call. If someone else is referencing the object
(e.g. through a sysfs file), they will obtain a reference to the
object, assume it's valid and operate on it. If the object is
unregistered and freed in the meantime, the operation will then
reference freed memory and go boom.
int kobject_uevent(struct kobject *kobj, enum kobject_action action);
This can be prevented, in the simplest case, by defining a release
method and freeing the object from there only. Note that this will not
secure reference count/object management models that use a dual
reference count or do other wacky things with the reference count
(like the networking layer).
Use the KOBJ_ADD action for when the kobject is first added to the kernel.
This should be done only after any attributes or children of the kobject
have been initialized properly, as userspace will instantly start to look
for them when this call happens.
When the kobject is removed from the kernel (details on how to do that is
below), the uevent for KOBJ_REMOVE will be automatically created by the
kobject core, so the caller does not have to worry about doing that by
hand.
1.4 sysfs
Reference counts
Each kobject receives a directory in sysfs. This directory is created
under the kobject's parent directory.
One of the key functions of a kobject is to serve as a reference counter
for the object in which it is embedded. As long as references to the object
exist, the object (and the code which supports it) must continue to exist.
The low-level functions for manipulating a kobject's reference counts are:
If a kobject does not have a parent when it is registered, its parent
becomes its dominant kset.
struct kobject *kobject_get(struct kobject *kobj);
void kobject_put(struct kobject *kobj);
If a kobject does not have a parent nor a dominant kset, its directory
is created at the top-level of the sysfs partition.
A successful call to kobject_get() will increment the kobject's reference
counter and return the pointer to the kobject.
When a reference is released, the call to kobject_put() will decrement the
reference count and, possibly, free the object. Note that kobject_init()
sets the reference count to one, so the code which sets up the kobject will
need to do a kobject_put() eventually to release that reference.
Because kobjects are dynamic, they must not be declared statically or on
the stack, but instead, always allocated dynamically. Future versions of
the kernel will contain a run-time check for kobjects that are created
statically and will warn the developer of this improper usage.
If all that you want to use a kobject for is to provide a reference counter
for your structure, please use the struct kref instead; a kobject would be
overkill. For more information on how to use struct kref, please see the
file Documentation/kref.txt in the Linux kernel source tree.
Creating "simple" kobjects
Sometimes all that a developer wants is a way to create a simple directory
in the sysfs hierarchy, and not have to mess with the whole complication of
ksets, show and store functions, and other details. This is the one
exception where a single kobject should be created. To create such an
entry, use the function:
struct kobject *kobject_create_and_add(char *name, struct kobject *parent);
This function will create a kobject and place it in sysfs in the location
underneath the specified parent kobject. To create simple attributes
associated with this kobject, use:
int sysfs_create_file(struct kobject *kobj, struct attribute *attr);
or
int sysfs_create_group(struct kobject *kobj, struct attribute_group *grp);
Both types of attributes used here, with a kobject that has been created
with the kobject_create_and_add(), can be of type kobj_attribute, so no
special custom attribute is needed to be created.
See the example module, samples/kobject/kobject-example.c for an
implementation of a simple kobject and attributes.
2. ksets
ktypes and release methods
2.1 Description
One important thing still missing from the discussion is what happens to a
kobject when its reference count reaches zero. The code which created the
kobject generally does not know when that will happen; if it did, there
would be little point in using a kobject in the first place. Even
predictable object lifecycles become more complicated when sysfs is brought
in as other portions of the kernel can get a reference on any kobject that
is registered in the system.
A kset is a set of kobjects that are embedded in the same type.
The end result is that a structure protected by a kobject cannot be freed
before its reference count goes to zero. The reference count is not under
the direct control of the code which created the kobject. So that code must
be notified asynchronously whenever the last reference to one of its
kobjects goes away.
Once you registered your kobject via kobject_add(), you must never use
kfree() to free it directly. The only safe way is to use kobject_put(). It
is good practice to always use kobject_put() after kobject_init() to avoid
errors creeping in.
This notification is done through a kobject's release() method. Usually
such a method has a form like:
void my_object_release(struct kobject *kobj)
{
struct my_object *mine = container_of(kobj, struct my_object, kobj);
/* Perform any additional cleanup on this object, then... */
kfree(mine);
}
One important point cannot be overstated: every kobject must have a
release() method, and the kobject must persist (in a consistent state)
until that method is called. If these constraints are not met, the code is
flawed. Note that the kernel will warn you if you forget to provide a
release() method. Do not try to get rid of this warning by providing an
"empty" release function; you will be mocked mercilessly by the kobject
maintainer if you attempt this.
Note, the name of the kobject is available in the release function, but it
must NOT be changed within this callback. Otherwise there will be a memory
leak in the kobject core, which makes people unhappy.
Interestingly, the release() method is not stored in the kobject itself;
instead, it is associated with the ktype. So let us introduce struct
kobj_type:
struct kobj_type {
void (*release)(struct kobject *);
struct sysfs_ops *sysfs_ops;
struct attribute **default_attrs;
};
This structure is used to describe a particular type of kobject (or, more
correctly, of containing object). Every kobject needs to have an associated
kobj_type structure; a pointer to that structure must be specified when you
call kobject_init() or kobject_init_and_add().
The release field in struct kobj_type is, of course, a pointer to the
release() method for this type of kobject. The other two fields (sysfs_ops
and default_attrs) control how objects of this type are represented in
sysfs; they are beyond the scope of this document.
The default_attrs pointer is a list of default attributes that will be
automatically created for any kobject that is registered with this ktype.
struct kset {
struct kobj_type * ktype;
struct list_head list;
struct kobject kobj;
struct kset_uevent_ops * uevent_ops;
ksets
A kset is merely a collection of kobjects that want to be associated with
each other. There is no restriction that they be of the same ktype, but be
very careful if they are not.
A kset serves these functions:
- It serves as a bag containing a group of objects. A kset can be used by
the kernel to track "all block devices" or "all PCI device drivers."
- A kset is also a subdirectory in sysfs, where the associated kobjects
with the kset can show up. Every kset contains a kobject which can be
set up to be the parent of other kobjects; the top-level directories of
the sysfs hierarchy are constructed in this way.
- Ksets can support the "hotplugging" of kobjects and influence how
uevent events are reported to user space.
In object-oriented terms, "kset" is the top-level container class; ksets
contain their own kobject, but that kobject is managed by the kset code and
should not be manipulated by any other user.
A kset keeps its children in a standard kernel linked list. Kobjects point
back to their containing kset via their kset field. In almost all cases,
the kobjects belonging to a ket have that kset (or, strictly, its embedded
kobject) in their parent.
As a kset contains a kobject within it, it should always be dynamically
created and never declared statically or on the stack. To create a new
kset use:
struct kset *kset_create_and_add(const char *name,
struct kset_uevent_ops *u,
struct kobject *parent);
When you are finished with the kset, call:
void kset_unregister(struct kset *kset);
to destroy it.
An example of using a kset can be seen in the
samples/kobject/kset-example.c file in the kernel tree.
If a kset wishes to control the uevent operations of the kobjects
associated with it, it can use the struct kset_uevent_ops to handle it:
struct kset_uevent_ops {
int (*filter)(struct kset *kset, struct kobject *kobj);
const char *(*name)(struct kset *kset, struct kobject *kobj);
int (*uevent)(struct kset *kset, struct kobject *kobj,
struct kobj_uevent_env *env);
};
void kset_init(struct kset * k);
int kset_add(struct kset * k);
int kset_register(struct kset * k);
void kset_unregister(struct kset * k);
The filter function allows a kset to prevent a uevent from being emitted to
userspace for a specific kobject. If the function returns 0, the uevent
will not be emitted.
struct kset * kset_get(struct kset * k);
void kset_put(struct kset * k);
The name function will be called to override the default name of the kset
that the uevent sends to userspace. By default, the name will be the same
as the kset itself, but this function, if present, can override that name.
struct kobject * kset_find_obj(struct kset *, char *);
The uevent function will be called when the uevent is about to be sent to
userspace to allow more environment variables to be added to the uevent.
One might ask how, exactly, a kobject is added to a kset, given that no
functions which perform that function have been presented. The answer is
that this task is handled by kobject_add(). When a kobject is passed to
kobject_add(), its kset member should point to the kset to which the
kobject will belong. kobject_add() will handle the rest.
If the kobject belonging to a kset has no parent kobject set, it will be
added to the kset's directory. Not all members of a kset do necessarily
live in the kset directory. If an explicit parent kobject is assigned
before the kobject is added, the kobject is registered with the kset, but
added below the parent kobject.
The type that the kobjects are embedded in is described by the ktype
pointer.
Kobject removal
A kset contains a kobject itself, meaning that it may be registered in
the kobject hierarchy and exported via sysfs. More importantly, the
kset may be embedded in a larger data type, and may be part of another
kset (of that object type).
After a kobject has been registered with the kobject core successfully, it
must be cleaned up when the code is finished with it. To do that, call
kobject_put(). By doing this, the kobject core will automatically clean up
all of the memory allocated by this kobject. If a KOBJ_ADD uevent has been
sent for the object, a corresponding KOBJ_REMOVE uevent will be sent, and
any other sysfs housekeeping will be handled for the caller properly.
For example, a block device is an object (struct gendisk) that is
contained in a set of block devices. It may also contain a set of
partitions (struct hd_struct) that have been found on the device. The
following code snippet illustrates how to express this properly.
If you need to do a two-stage delete of the kobject (say you are not
allowed to sleep when you need to destroy the object), then call
kobject_del() which will unregister the kobject from sysfs. This makes the
kobject "invisible", but it is not cleaned up, and the reference count of
the object is still the same. At a later time call kobject_put() to finish
the cleanup of the memory associated with the kobject.
struct gendisk * disk;
...
disk->kset.kobj.kset = &block_kset;
disk->kset.ktype = &partition_ktype;
kset_register(&disk->kset);
- The kset that the disk's embedded object belongs to is the
block_kset, and is pointed to by disk->kset.kobj.kset.
- The type of objects on the disk's _subordinate_ list are partitions,
and is set in disk->kset.ktype.
- The kset is then registered, which handles initializing and adding
the embedded kobject to the hierarchy.
kobject_del() can be used to drop the reference to the parent object, if
circular references are constructed. It is valid in some cases, that a
parent objects references a child. Circular references _must_ be broken
with an explicit call to kobject_del(), so that a release functions will be
called, and the objects in the former circle release each other.
2.2 kset Programming Interface
All kset functions, except kset_find_obj(), eventually forward the
calls to their embedded kobjects after performing kset-specific
operations. ksets offer a similar programming model to kobjects: they
may be used after they are initialized, without registering them in
the hierarchy.
kset_find_obj() may be used to locate a kobject with a particular
name. The kobject, if found, is returned.
There are also some helper functions which names point to the formerly
existing "struct subsystem", whose functions have been taken over by
ksets.
decl_subsys(name,type,uevent_ops)
Declares a kset named '<name>_subsys' of type <type> with
uevent_ops <uevent_ops>. For example,
decl_subsys(devices, &ktype_device, &device_uevent_ops);
is equivalent to doing:
struct kset devices_subsys = {
.ktype = &ktype_devices,
.uevent_ops = &device_uevent_ops,
};
kobject_set_name(&devices_subsys, name);
The objects that are registered with a subsystem that use the
subsystem's default list must have their kset ptr set properly. These
objects may have embedded kobjects or ksets. The
following helper makes setting the kset easier:
kobj_set_kset_s(obj,subsys)
- Assumes that obj->kobj exists, and is a struct kobject.
- Sets the kset of that kobject to the kset <subsys>.
int subsystem_register(struct kset *s);
void subsystem_unregister(struct kset *s);
These are just wrappers around the respective kset_* functions.
2.3 sysfs
ksets are represented in sysfs when their embedded kobjects are
registered. They follow the same rules of parenting, with one
exception. If a kset does not have a parent, nor is its embedded
kobject part of another kset, the kset's parent becomes its dominant
subsystem.
If the kset does not have a parent, its directory is created at the
sysfs root. This should only happen when the kset registered is
embedded in a subsystem itself.
3. struct ktype
3.1. Description
struct kobj_type {
void (*release)(struct kobject *);
struct sysfs_ops * sysfs_ops;
struct attribute ** default_attrs;
};
Object types require specific functions for converting between the
generic object and the more complex type. struct kobj_type provides
the object-specific fields, which include:
- release: Called when the kobject's reference count reaches 0. This
should convert the object to the more complex type and free it.
- sysfs_ops: Provides conversion functions for sysfs access. Please
see the sysfs documentation for more information.
- default_attrs: Default attributes to be exported via sysfs when the
object is registered.Note that the last attribute has to be
initialized to NULL ! You can find a complete implementation
in block/genhd.c
Instances of struct kobj_type are not registered; only referenced by
the kset. A kobj_type may be referenced by an arbitrary number of
ksets, as there may be disparate sets of identical objects.
Example code to copy from
For a more complete example of using ksets and kobjects properly, see the
sample/kobject/kset-example.c code.

View file

@ -17,9 +17,9 @@ The User Interface
------------------
The Linux Plug and Play user interface provides a means to activate PnP devices
for legacy and user level drivers that do not support Linux Plug and Play. The
user interface is integrated into driverfs.
user interface is integrated into sysfs.
In addition to the standard driverfs file the following are created in each
In addition to the standard sysfs file the following are created in each
device's directory:
id - displays a list of support EISA IDs
options - displays possible resource configurations

View file

@ -133,7 +133,7 @@ During its startup the Linux/390 system checks for peripheral devices. Each
of those devices is uniquely defined by a so called subchannel by the ESA/390
channel subsystem. While the subchannel numbers are system generated, each
subchannel also takes a user defined attribute, the so called device number.
Both subchannel number and device number cannot exceed 65535. During driverfs
Both subchannel number and device number cannot exceed 65535. During sysfs
initialisation, the information about control unit type and device types that
imply specific I/O commands (channel command words - CCWs) in order to operate
the device are gathered. Device drivers can retrieve this set of hardware

View file

@ -1021,7 +1021,7 @@ void read_slab_dir(void)
char *t;
int count;
if (chdir("/sys/slab"))
if (chdir("/sys/kernel/slab"))
fatal("SYSFS support for SLUB not active\n");
dir = opendir(".");

View file

@ -63,7 +63,7 @@ In case you forgot to enable debugging on the kernel command line: It is
possible to enable debugging manually when the kernel is up. Look at the
contents of:
/sys/slab/<slab name>/
/sys/kernel/slab/<slab name>/
Look at the writable files. Writing 1 to them will enable the
corresponding debug option. All options can be set on a slab that does

View file

@ -0,0 +1,701 @@
Chinese translated version of Documentation/CodingStyle
If you have any comment or update to the content, please post to LKML directly.
However, if you have problem communicating in English you can also ask the
Chinese maintainer for help. Contact the Chinese maintainer, if this
translation is outdated or there is problem with translation.
Chinese maintainer: Zhang Le <r0bertz@gentoo.org>
---------------------------------------------------------------------
Documentation/CodingStyle的中文翻译
如果想评论或更新本文的内容请直接发信到LKML。如果你使用英文交流有困难的话也可
以向中文版维护者求助。如果本翻译更新不及时或者翻译存在问题,请联系中文版维护者。
中文版维护者: 张乐 Zhang Le <r0bertz@gentoo.org>
中文版翻译者: 张乐 Zhang Le <r0bertz@gentoo.org>
中文版校译者: 王聪 Wang Cong <xiyou.wangcong@gmail.com>
wheelz <kernel.zeng@gmail.com>
管旭东 Xudong Guan <xudong.guan@gmail.com>
Li Zefan <lizf@cn.fujitsu.com>
Wang Chen <wangchen@cn.fujitsu.com>
以下为正文
---------------------------------------------------------------------
Linux内核代码风格
这是一个简短的文档描述了linux内核的首选代码风格。代码风格是因人而异的而且我
不愿意把我的观点强加给任何人,不过这里所讲述的是我必须要维护的代码所遵守的风格,
并且我也希望绝大多数其他代码也能遵守这个风格。请在写代码时至少考虑一下本文所述的
风格。
首先我建议你打印一份GNU代码规范然后不要读它。烧了它这是一个具有重大象征性
意义的动作。
不管怎样,现在我们开始:
第一章:缩进
制表符是8个字符所以缩进也是8个字符。有些异端运动试图将缩进变为4乃至2个字符
这几乎相当于尝试将圆周率的值定义为3。
理由:缩进的全部意义就在于清楚的定义一个控制块起止于何处。尤其是当你盯着你的屏幕
连续看了20小时之后你将会发现大一点的缩进会使你更容易分辨缩进。
现在有些人会抱怨8个字符的缩进会使代码向右边移动的太远在80个字符的终端屏幕上
就很难读这样的代码。这个问题的答案是如果你需要3级以上的缩进不管用何种方式你
的代码已经有问题了,应该修正你的程序。
简而言之8个字符的缩进可以让代码更容易阅读还有一个好处是当你的函数嵌套太深的
时候可以给你警告。留心这个警告。
在switch语句中消除多级缩进的首选的方式是让“switch”和从属于它的“case”标签对齐于同
一列而不要“两次缩进”“case”标签。比如
switch (suffix) {
case 'G':
case 'g':
mem <<= 30;
break;
case 'M':
case 'm':
mem <<= 20;
break;
case 'K':
case 'k':
mem <<= 10;
/* fall through */
default:
break;
}
不要把多个语句放在一行里,除非你有什么东西要隐藏:
if (condition) do_this;
do_something_everytime;
也不要在一行里放多个赋值语句。内核代码风格超级简单。就是避免可能导致别人误读的表
达式。
除了注释、文档和Kconfig之外不要使用空格来缩进前面的例子是例外是有意为之。
选用一个好的编辑器,不要在行尾留空格。
第二章:把长的行和字符串打散
代码风格的意义就在于使用平常使用的工具来维持代码的可读性和可维护性。
每一行的长度的限制是80列我们强烈建议您遵守这个惯例。
长于80列的语句要打散成有意义的片段。每个片段要明显短于原来的语句而且放置的位置
也明显的靠右。同样的规则也适用于有很长参数列表的函数头。长字符串也要打散成较短的
字符串。唯一的例外是超过80列可以大幅度提高可读性并且不会隐藏信息的情况。
void fun(int a, int b, int c)
{
if (condition)
printk(KERN_WARNING "Warning this is a long printk with "
"3 parameters a: %u b: %u "
"c: %u \n", a, b, c);
else
next_statement;
}
第三章:大括号和空格的放置
C语言风格中另外一个常见问题是大括号的放置。和缩进大小不同选择或弃用某种放置策
略并没有多少技术上的原因不过首选的方式就像Kernighan和Ritchie展示给我们的
把起始大括号放在行尾,而把结束大括号放在行首,所以:
if (x is true) {
we do y
}
这适用于所有的非函数语句块if、switch、for、while、do。比如
switch (action) {
case KOBJ_ADD:
return "add";
case KOBJ_REMOVE:
return "remove";
case KOBJ_CHANGE:
return "change";
default:
return NULL;
}
不过,有一个例外,那就是函数:函数的起始大括号放置于下一行的开头,所以:
int function(int x)
{
body of function
}
全世界的异端可能会抱怨这个不一致性是……呃……不一致的,不过所有思维健全的人都知道(
aK&R是_正确的_并且bK&R是正确的。此外不管怎样函数都是特殊的在C语言中
,函数是不能嵌套的)。
注意结束大括号独自占据一行除非它后面跟着同一个语句的剩余部分也就是do语句中的
“while”或者if语句中的“else”像这样
do {
body of do-loop
} while (condition);
if (x == y) {
..
} else if (x > y) {
...
} else {
....
}
理由K&R。
也请注意这种大括号的放置方式也能使空(或者差不多空的)行的数量最小化,同时不失可
读性。因此由于你的屏幕上的新行是不可再生资源想想25行的终端屏幕你将会有更
多的空行来放置注释。
当只有一个单独的语句的时候,不用加不必要的大括号。
if (condition)
action();
这点不适用于本身为某个条件语句的一个分支的单独语句。这时需要在两个分支里都使用大
括号。
if (condition) {
do_this();
do_that();
} else {
otherwise();
}
3.1:空格
Linux内核的空格使用方式主要取决于它是用于函数还是关键字。大多数关键字后
要加一个空格。值得注意的例外是sizeof、typeof、alignof和__attribute__这些关键字
某些程度上看起来更像函数它们在Linux里也常常伴随小括号而使用尽管在C语言里这样
的小括号不是必需的就像“struct fileinfo info”声明过后的“sizeof info”
所以在这些关键字之后放一个空格:
if, switch, case, for, do, while
但是不要在sizeof、typeof、alignof或者__attribute__这些关键字之后放空格。例如
s = sizeof(struct file);
不要在小括号里的表达式两侧加空格。这是一个反例:
s = sizeof( struct file );
当声明指针类型或者返回指针类型的函数时,“*”的首选使用方式是使之靠近变量名或者函
数名,而不是靠近类型名。例子:
char *linux_banner;
unsigned long long memparse(char *ptr, char **retptr);
char *match_strdup(substring_t *s);
在大多数二元和三元操作符两侧使用一个空格,例如下面所有这些操作符:
= + - < > * / % | & ^ <= >= == != ? :
但是一元操作符后不要加空格:
& * + - ~ ! sizeof typeof alignof __attribute__ defined
后缀自加和自减一元操作符前不加空格:
++ --
前缀自加和自减一元操作符后不加空格:
++ --
“.”和“->”结构体成员操作符前后不加空格。
不要在行尾留空白。有些可以自动缩进的编辑器会在新行的行首加入适量的空白,然后你
就可以直接在那一行输入代码。不过假如你最后没有在那一行输入代码,有些编辑器就不
会移除已经加入的空白,就像你故意留下一个只有空白的行。包含行尾空白的行就这样产
生了。
当git发现补丁包含了行尾空白的时候会警告你并且可以应你的要求去掉行尾空白不过
如果你是正在打一系列补丁,这样做会导致后面的补丁失败,因为你改变了补丁的上下文。
第四章:命名
C是一个简朴的语言你的命名也应该这样。和Modula-2和Pascal程序员不同C程序员不使
用类似ThisVariableIsATemporaryCounter这样华丽的名字。C程序员会称那个变量为“tmp”
,这样写起来会更容易,而且至少不会令其难于理解。
不过,虽然混用大小写的名字是不提倡使用的,但是全局变量还是需要一个具描述性的名字
。称一个全局函数为“foo”是一个难以饶恕的错误。
全局变量(只有当你真正需要它们的时候再用它)需要有一个具描述性的名字,就像全局函
数。如果你有一个可以计算活动用户数量的函数你应该叫它“count_active_users()”或者
类似的名字你不应该叫它“cntuser()”。
在函数名中包含函数类型(所谓的匈牙利命名法)是脑子出了问题——编译器知道那些类型而
且能够检查那些类型,这样做只能把程序员弄糊涂了。难怪微软总是制造出有问题的程序。
本地变量名应该简短,而且能够表达相关的含义。如果你有一些随机的整数型的循环计数器
它应该被称为“i”。叫它“loop_counter”并无益处如果它没有被误解的可能的话。类似
“tmp”可以用来称呼任意类型的临时变量。
如果你怕混淆了你的本地变量名,你就遇到另一个问题了,叫做函数增长荷尔蒙失衡综合症
。请看第六章(函数)。
第五章Typedef
不要使用类似“vps_t”之类的东西。
对结构体和指针使用typedef是一个错误。当你在代码里看到
vps_t a;
这代表什么意思呢?
相反,如果是这样
struct virtual_container *a;
你就知道“a”是什么了。
很多人认为typedef“能提高可读性”。实际不是这样的。它们只在下列情况下有用
(a) 完全不透明的对象这种情况下要主动使用typedef来隐藏这个对象实际上是什么
例如“pte_t”等不透明对象你只能用合适的访问函数来访问它们。
注意不透明性和“访问函数”本身是不好的。我们使用pte_t等类型的原因在于真的是
完全没有任何共用的可访问信息。
(b) 清楚的整数类型如此这层抽象就可以帮助消除到底是“int”还是“long”的混淆。
u8/u16/u32是完全没有问题的typedef不过它们更符合类别(d)而不是这里。
再次注意要这样做必须事出有因。如果某个变量是“unsigned long“那么没有必要
typedef unsigned long myflags_t;
不过如果有一个明确的原因比如它在某种情况下可能会是一个“unsigned int”而在
其他情况下可能为“unsigned long”那么就不要犹豫请务必使用typedef。
(c) 当你使用sparse按字面的创建一个新类型来做类型检查的时候。
(d) 和标准C99类型相同的类型在某些例外的情况下。
虽然让眼睛和脑筋来适应新的标准类型比如“uint32_t”不需要花很多时间可是有些
人仍然拒绝使用它们。
因此Linux特有的等同于标准类型的“u8/u16/u32/u64”类型和它们的有符号类型是被
允许的——尽管在你自己的新代码中,它们不是强制要求要使用的。
当编辑已经使用了某个类型集的已有代码时,你应该遵循那些代码中已经做出的选择。
(e) 可以在用户空间安全使用的类型。
在某些用户空间可见的结构体里我们不能要求C99类型而且不能用上面提到的“u32”
类型。因此我们在与用户空间共享的所有结构体中使用__u32和类似的类型。
可能还有其他的情况不过基本的规则是永远不要使用typedef除非你可以明确的应用上
述某个规则中的一个。
总的来说,如果一个指针或者一个结构体里的元素可以合理的被直接访问到,那么它们就不
应该是一个typedef。
第六章:函数
函数应该简短而漂亮,并且只完成一件事情。函数应该可以一屏或者两屏显示完(我们都知
道ISO/ANSI屏幕大小是80x24只做一件事情而且把它做好。
一个函数的最大长度是和该函数的复杂度和缩进级数成反比的。所以,如果你有一个理论上
很简单的只有一个很长但是简单的case语句的函数而且你需要在每个case里做很多很
小的事情,这样的函数尽管很长,但也是可以的。
不过,如果你有一个复杂的函数,而且你怀疑一个天分不是很高的高中一年级学生可能甚至
搞不清楚这个函数的目的,你应该严格的遵守前面提到的长度限制。使用辅助函数,并为之
取个具描述性的名字(如果你觉得它们的性能很重要的话,可以让编译器内联它们,这样的
效果往往会比你写一个复杂函数的效果要好。)
函数的另外一个衡量标准是本地变量的数量。此数量不应超过510个否则你的函数就有
问题了。重新考虑一下你的函数,把它分拆成更小的函数。人的大脑一般可以轻松的同时跟
踪7个不同的事物如果再增多的话就会糊涂了。即便你聪颖过人你也可能会记不清你2
个星期前做过的事情。
在源文件里使用空行隔开不同的函数。如果该函数需要被导出它的EXPORT*宏应该紧贴
在它的结束大括号之下。比如:
int system_is_up(void)
{
return system_state == SYSTEM_RUNNING;
}
EXPORT_SYMBOL(system_is_up);
在函数原型中包含函数名和它们的数据类型。虽然C语言里没有这样的要求在Linux里这
是提倡的做法,因为这样可以很简单的给读者提供更多的有价值的信息。
第七章:集中的函数退出途径
虽然被某些人声称已经过时但是goto语句的等价物还是经常被编译器所使用具体形式是
无条件跳转指令。
当一个函数从多个位置退出并且需要做一些通用的清理工作的时候goto的好处就显现出来
了。
理由是:
- 无条件语句容易理解和跟踪
- 嵌套程度减小
- 可以避免由于修改时忘记更新某个单独的退出点而导致的错误
- 减轻了编译器的工作,无需删除冗余代码;)
int fun(int a)
{
int result = 0;
char *buffer = kmalloc(SIZE);
if (buffer == NULL)
return -ENOMEM;
if (condition1) {
while (loop1) {
...
}
result = 1;
goto out;
}
...
out:
kfree(buffer);
return result;
}
第八章:注释
注释是好的,不过有过度注释的危险。永远不要在注释里解释你的代码是如何运作的:更好
的做法是让别人一看你的代码就可以明白,解释写的很差的代码是浪费时间。
一般的,你想要你的注释告诉别人你的代码做了什么,而不是怎么做的。也请你不要把注释
放在一个函数体内部:如果函数复杂到你需要独立的注释其中的一部分,你很可能需要回到
第六章看一看。你可以做一些小注释来注明或警告某些很聪明(或者槽糕)的做法,但不要
加太多。你应该做的,是把注释放在函数的头部,告诉人们它做了什么,也可以加上它做这
些事情的原因。
当注释内核API函数时请使用kernel-doc格式。请看
Documentation/kernel-doc-nano-HOWTO.txt和scripts/kernel-doc以获得详细信息。
Linux的注释风格是C89“/* ... */”风格。不要使用C99风格“// ...”注释。
长(多行)的首选注释风格是:
/*
* This is the preferred style for multi-line
* comments in the Linux kernel source code.
* Please use it consistently.
*
* Description: A column of asterisks on the left side,
* with beginning and ending almost-blank lines.
*/
注释数据也是很重要的,不管是基本类型还是衍生类型。为了方便实现这一点,每一行应只
声明一个数据(不要使用逗号来一次声明多个数据)。这样你就有空间来为每个数据写一段
小注释来解释它们的用途了。
第九章:你已经把事情弄糟了
这没什么我们都是这样。可能你的使用了很长时间Unix的朋友已经告诉你“GNU emacs”能
自动帮你格式化C源代码而且你也注意到了确实是这样不过它所使用的默认值和我们
想要的相去甚远实际上甚至比随机打的还要差——无数个猴子在GNU emacs里打字永远不
会创造出一个好程序译注请参考Infinite Monkey Theorem
所以你要么放弃GNU emacs要么改变它让它使用更合理的设定。要采用后一个方案你可
以把下面这段粘贴到你的.emacs文件里。
(defun linux-c-mode ()
"C mode with adjusted defaults for use with the Linux kernel."
(interactive)
(c-mode)
(c-set-style "K&R")
(setq tab-width 8)
(setq indent-tabs-mode t)
(setq c-basic-offset 8))
这样就定义了M-x linux-c-mode命令。当你hack一个模块的时候如果你把字符串
-*- linux-c -*-放在头两行的某个位置,这个模式将会被自动调用。如果你希望在你修改
/usr/src/linux里的文件时魔术般自动打开linux-c-mode的话你也可能需要添加
(setq auto-mode-alist (cons '("/usr/src/linux.*/.*\\.[ch]$" . linux-c-mode)
auto-mode-alist))
到你的.emacs文件里。
不过就算你尝试让emacs正确的格式化代码失败了也并不意味着你失去了一切还可以用“
indent”。
不过GNU indent也有和GNU emacs一样有问题的设定所以你需要给它一些命令选项。不
这还不算太糟糕因为就算是GNU indent的作者也认同K&R的权威性GNU的人并不是坏
他们只是在这个问题上被严重的误导了所以你只要给indent指定选项“-kr -i8”
代表“K&R8个字符缩进”或者使用“scripts/Lindent”这样就可以以最时髦的方式
缩进源代码。
“indent”有很多选项特别是重新格式化注释的时候你可能需要看一下它的手册页。不过
记住“indent”不能修正坏的编程习惯。
第十章Kconfig配置文件
对于遍布源码树的所有Kconfig*配置文件来说它们缩进方式与C代码相比有所不同。紧挨
在“config”定义下面的行缩进一个制表符帮助信息则再多缩进2个空格。比如
config AUDIT
bool "Auditing support"
depends on NET
help
Enable auditing infrastructure that can be used with another
kernel subsystem, such as SELinux (which requires this for
logging of avc messages output). Does not do system-call
auditing without CONFIG_AUDITSYSCALL.
仍然被认为不够稳定的功能应该被定义为依赖于“EXPERIMENTAL”
config SLUB
depends on EXPERIMENTAL && !ARCH_USES_SLAB_PAGE_STRUCT
bool "SLUB (Unqueued Allocator)"
...
而那些危险的功能(比如某些文件系统的写支持)应该在它们的提示字符串里显著的声明这
一点:
config ADFS_FS_RW
bool "ADFS write support (DANGEROUS)"
depends on ADFS_FS
...
要查看配置文件的完整文档请看Documentation/kbuild/kconfig-language.txt。
第十一章:数据结构
如果一个数据结构,在创建和销毁它的单线执行环境之外可见,那么它必须要有一个引用计
数器。内核里没有垃圾收集(并且内核之外的垃圾收集慢且效率低下),这意味着你绝对需
要记录你对这种数据结构的使用情况。
引用计数意味着你能够避免上锁,并且允许多个用户并行访问这个数据结构——而不需要担心
这个数据结构仅仅因为暂时不被使用就消失了,那些用户可能不过是沉睡了一阵或者做了一
些其他事情而已。
注意上锁不能取代引用计数。上锁是为了保持数据结构的一致性,而引用计数是一个内存管
理技巧。通常二者都需要,不要把两个搞混了。
很多数据结构实际上有2级引用计数它们通常有不同“类”的用户。子类计数器统计子类用
户的数量,每当子类计数器减至零时,全局计数器减一。
这种“多级引用计数”的例子可以在内存管理“struct mm_struct”mm_users和mm_count
和文件系统“struct super_block”s_count和s_active中找到。
记住:如果另一个执行线索可以找到你的数据结构,但是这个数据结构没有引用计数器,这
里几乎肯定是一个bug。
第十二章枚举和RTL
用于定义常量的宏的名字及枚举里的标签需要大写。
#define CONSTANT 0x12345
在定义几个相关的常量时,最好用枚举。
宏的名字请用大写字母,不过形如函数的宏的名字可以用小写字母。
一般的,如果能写成内联函数就不要写成像函数的宏。
含有多个语句的宏应该被包含在一个do-while代码块里
#define macrofun(a, b, c) \
do { \
if (a == 5) \
do_this(b, c); \
} while (0)
使用宏的时候应避免的事情:
1) 影响控制流程的宏:
#define FOO(x) \
do { \
if (blah(x) < 0) \
return -EBUGGERED; \
} while(0)
非常不好。它看起来像一个函数,不过却能导致“调用”它的函数退出;不要打乱读者大脑里
的语法分析器。
2) 依赖于一个固定名字的本地变量的宏:
#define FOO(val) bar(index, val)
可能看起来像是个不错的东西,不过它非常容易把读代码的人搞糊涂,而且容易导致看起来
不相关的改动带来错误。
3) 作为左值的带参数的宏: FOO(x) = y如果有人把FOO变成一个内联函数的话这种用
法就会出错了。
4) 忘记了优先级:使用表达式定义常量的宏必须将表达式置于一对小括号之内。带参数的
宏也要注意此类问题。
#define CONSTANT 0x4000
#define CONSTEXP (CONSTANT | 3)
cpp手册对宏的讲解很详细。Gcc internals手册也详细讲解了RTL译注register
transfer language内核里的汇编语言经常用到它。
第十三章:打印内核消息
内核开发者应该是受过良好教育的。请一定注意内核信息的拼写,以给人以好的印象。不要
用不规范的单词比如“dont”而要用“do not”或者“don't”。保证这些信息简单、明了、无
歧义。
内核信息不必以句号(译注:英文句号,即点)结束。
在小括号里打印数字(%d)没有任何价值,应该避免这样做。
<linux/device.h>里有一些驱动模型诊断宏,你应该使用它们,以确保信息对应于正确的
设备和驱动并且被标记了正确的消息级别。这些宏有dev_err(), dev_warn(),
dev_info()等等。对于那些不和某个特定设备相关连的信息,<linux/kernel.h>定义了
pr_debug()和pr_info()。
写出好的调试信息可以是一个很大的挑战;当你写出来之后,这些信息在远程除错的时候
就会成为极大的帮助。当DEBUG符号没有被定义的时候这些信息不应该被编译进内核里
也就是说默认地它们不应该被包含在内。如果你使用dev_dbg()或者pr_debug()
就能自动达到这个效果。很多子系统拥有Kconfig选项来启用-DDEBUG。还有一个相关的惯例
是使用VERBOSE_DEBUG来添加dev_vdbg()消息到那些已经由DEBUG启用的消息之上。
第十四章:分配内存
内核提供了下面的一般用途的内存分配函数kmalloc()kzalloc()kcalloc()和
vmalloc()。请参考API文档以获取有关它们的详细信息。
传递结构体大小的首选形式是这样的:
p = kmalloc(sizeof(*p), ...);
另外一种传递方式中sizeof的操作数是结构体的名字这样会降低可读性并且可能会引
入bug。有可能指针变量类型被改变时而对应的传递给内存分配函数的sizeof的结果不变。
强制转换一个void指针返回值是多余的。C语言本身保证了从void指针到其他任何指针类型
的转换是没有问题的。
第十五章:内联弊病
有一个常见的误解是内联函数是gcc提供的可以让代码运行更快的一个选项。虽然使用内联
函数有时候是恰当的(比如作为一种替代宏的方式,请看第十二章),不过很多情况下不是
这样。inline关键字的过度使用会使内核变大从而使整个系统运行速度变慢。因为大内核
会占用更多的指令高速缓存(译注:一级缓存通常是指令缓存和数据缓存分开的)而且会导
致pagecache的可用内存减少。想象一下一次pagecache未命中就会导致一次磁盘寻址
耗时5毫秒。5毫秒的时间内CPU能执行很多很多指令。
一个基本的原则是如果一个函数有3行以上就不要把它变成内联函数。这个原则的一个例
外是,如果你知道某个参数是一个编译时常量,而且因为这个常量你确定编译器在编译时能
优化掉你的函数的大部分代码那仍然可以给它加上inline关键字。kmalloc()内联函数就
是一个很好的例子。
人们经常主张给static的而且只用了一次的函数加上inline如此不会有任何损失因为没
有什么好权衡的。虽然从技术上说这是正确的但是实际上这种情况下即使不加inline gcc
也可以自动使其内联。而且其他用户可能会要求移除inline由此而来的争论会抵消inline
自身的潜在价值,得不偿失。
第十六章:函数返回值及命名
函数可以返回很多种不同类型的值,最常见的一种是表明函数执行成功或者失败的值。这样
的一个值可以表示为一个错误代码整数(-Exxx失败0成功或者一个“成功”布尔值
0失败非0成功
混合使用这两种表达方式是难于发现的bug的来源。如果C语言本身严格区分整形和布尔型变
那么编译器就能够帮我们发现这些错误……不过C语言不区分。为了避免产生这种bug
遵循下面的惯例:
如果函数的名字是一个动作或者强制性的命令,那么这个函数应该返回错误代码整
数。如果是一个判断,那么函数应该返回一个“成功”布尔值。
比如“add work”是一个命令所以add_work()函数在成功时返回0在失败时返回-EBUSY。
类似的因为“PCI device present”是一个判断所以pci_dev_present()函数在成功找到
一个匹配的设备时应该返回1如果找不到时应该返回0。
所有导出译注EXPORT的函数都必须遵守这个惯例所有的公共函数也都应该如此。私
static函数不需要如此但是我们也推荐这样做。
返回值是实际计算结果而不是计算是否成功的标志的函数不受此惯例的限制。一般的,他们
通过返回一些正常值范围之外的结果来表示出错。典型的例子是返回指针的函数,他们使用
NULL或者ERR_PTR机制来报告错误。
第十七章:不要重新发明内核宏
头文件include/linux/kernel.h包含了一些宏你应该使用它们而不要自己写一些它们的
变种。比如,如果你需要计算一个数组的长度,使用这个宏
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
类似的,如果你要计算某结构体成员的大小,使用
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
还有可以做严格的类型检查的min()和max()宏,如果你需要可以使用它们。你可以自己看看
那个头文件里还定义了什么你可以拿来用的东西,如果有定义的话,你就不应在你的代码里
自己重新定义。
第十八章:编辑器模式行和其他需要罗嗦的事情
有一些编辑器可以解释嵌入在源文件里的由一些特殊标记标明的配置信息。比如emacs
能够解释被标记成这样的行:
-*- mode: c -*-
或者这样的:
/*
Local Variables:
compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c"
End:
*/
Vim能够解释这样的标记
/* vim:set sw=8 noet */
不要在源代码中包含任何这样的内容。每个人都有他自己的编辑器配置,你的源文件不应
该覆盖别人的配置。这包括有关缩进和模式配置的标记。人们可以使用他们自己定制的模
式,或者使用其他可以产生正确的缩进的巧妙方法。
附录 I参考
The C Programming Language, 第二版, 作者Brian W. Kernighan和Denni
M. Ritchie. Prentice Hall, Inc., 1988. ISBN 0-13-110362-8 (软皮),
0-13-110370-9 (硬皮). URL: http://cm.bell-labs.com/cm/cs/cbook/
The Practice of Programming 作者Brian W. Kernighan和Rob Pike. Addison-Wesley,
Inc., 1999. ISBN 0-201-61586-X. URL: http://cm.bell-labs.com/cm/cs/tpop/
cppgccgcc internals和indent的GNU手册——和K&R及本文相符合的部分全部可以在
http://www.gnu.org/manual/找到
WG14是C语言的国际标准化工作组URL: http://www.open-std.org/JTC1/SC22/WG14/
Kernel CodingStyle作者greg@kroah.com发表于OLS 2002
http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/
--
最后更新于2007年7月13日。

View file

@ -1,10 +1,10 @@
Chinese translated version of Documentation/HOWTO
If you have any comment or update to the content, please contact the
original document maintainer directly. However, if you have problem
original document maintainer directly. However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help. Contact the Chinese maintainer, if this translation is outdated
or there is problem with translation.
help. Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.
Maintainer: Greg Kroah-Hartman <greg@kroah.com>
Chinese maintainer: Li Yang <leoli@freescale.com>
@ -85,7 +85,7 @@ Linux内核源代码都是在GPL通用公共许可证的保护下发布的
Linux内核代码中包含有大量的文档。这些文档对于学习如何与内核社区互动有着
不可估量的价值。当一个新的功能被加入内核,最好把解释如何使用这个功能的文
档也放进内核。当内核的改动导致面向用户空间的接口发生变化时,最好将相关信
息或手册页(manpages)的补丁发到mtk-manpages@gmx.net,以向手册页(manpages)
息或手册页(manpages)的补丁发到mtk.manpages@gmail.com,以向手册页(manpages)
的维护者解释这些变化。
以下是内核代码中需要阅读的文档:
@ -218,6 +218,8 @@ kernel.org网站的pub/linux/kernel/v2.6/目录下找到它。它的开发遵循
时,一个新的-rc版本就会被发布。计划是每周都发布新的-rc版本。
- 这个过程一直持续下去直到内核被认为达到足够稳定的状态,持续时间大概是
6个星期。
- 以下地址跟踪了在每个-rc发布中发现的退步列表
http://kernelnewbies.org/known_regressions
关于内核发布值得一提的是Andrew Morton在linux-kernel邮件列表中如是说
“没有人知道新内核何时会被发布因为发布是根据已知bug的情况来决定

View file

@ -0,0 +1,168 @@
Chinese translated version of Documentation/SubmittingDrivers
If you have any comment or update to the content, please contact the
original document maintainer directly. However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help. Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.
Chinese maintainer: Li Yang <leo@zh-kernel.org>
---------------------------------------------------------------------
Documentation/SubmittingDrivers 的中文翻译
如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
译存在问题,请联系中文版维护者。
中文版维护者: 李阳 Li Yang <leo@zh-kernel.org>
中文版翻译者: 李阳 Li Yang <leo@zh-kernel.org>
中文版校译者: 陈琦 Maggie Chen <chenqi@beyondsoft.com>
王聪 Wang Cong <xiyou.wangcong@gmail.com>
张巍 Zhang Wei <Wei.Zhang@freescale.com>
以下为正文
---------------------------------------------------------------------
如何向 Linux 内核提交驱动程序
-----------------------------
这篇文档将会解释如何向不同的内核源码树提交设备驱动程序。请注意,如果你感
兴趣的是显卡驱动程序,你也许应该访问 XFree86 项目(http://www.xfree86.org/)
和/或 X.org 项目 (http://x.org)。
另请参阅 Documentation/SubmittingPatches 文档。
分配设备号
----------
块设备和字符设备的主设备号与从设备号是由 Linux 命名编号分配权威 LANANA
现在是 Torben Mathiasen负责分配。申请的网址是 http://www.lanana.org/。
即使不准备提交到主流内核的设备驱动也需要在这里分配设备号。有关详细信息,
请参阅 Documentation/devices.txt。
如果你使用的不是已经分配的设备号,那么当你提交设备驱动的时候,它将会被强
制分配一个新的设备号,即便这个设备号和你之前发给客户的截然不同。
设备驱动的提交对象
------------------
Linux 2.0:
此内核源码树不接受新的驱动程序。
Linux 2.2:
此内核源码树不接受新的驱动程序。
Linux 2.4:
如果所属的代码领域在内核的 MAINTAINERS 文件中列有一个总维护者,
那么请将驱动程序提交给他。如果此维护者没有回应或者你找不到恰当的
维护者,那么请联系 Willy Tarreau <w@1wt.eu>。
Linux 2.6:
除了遵循和 2.4 版内核同样的规则外,你还需要在 linux-kernel 邮件
列表上跟踪最新的 API 变化。向 Linux 2.6 内核提交驱动的顶级联系人
是 Andrew Morton <akpm@osdl.org>。
决定设备驱动能否被接受的条件
----------------------------
许可: 代码必须使用 GNU 通用公开许可证 (GPL) 提交给 Linux但是
我们并不要求 GPL 是唯一的许可。你或许会希望同时使用多种
许可证发布如果希望驱动程序可以被其他开源社区比如BSD
使用。请参考 include/linux/module.h 文件中所列出的可被
接受共存的许可。
版权: 版权所有者必须同意使用 GPL 许可。最好提交者和版权所有者
是相同个人或实体。否则,必需列出授权使用 GPL 的版权所有
人或实体,以备验证之需。
接口: 如果你的驱动程序使用现成的接口并且和其他同类的驱动程序行
为相似,而不是去发明无谓的新接口,那么它将会更容易被接受。
如果你需要一个 Linux 和 NT 的通用驱动接口,那么请在用
户空间实现它。
代码: 请使用 Documentation/CodingStyle 中所描述的 Linux 代码风
格。如果你的某些代码段(例如那些与 Windows 驱动程序包共
享的代码段)需要使用其他格式,而你却只希望维护一份代码,
那么请将它们很好地区分出来,并且注明原因。
可移植性: 请注意,指针并不永远是 32 位的,不是所有的计算机都使用小
尾模式 (little endian) 存储数据,不是所有的人都拥有浮点
单元,不要随便在你的驱动程序里嵌入 x86 汇编指令。只能在
x86 上运行的驱动程序一般是不受欢迎的。虽然你可能只有 x86
硬件,很难测试驱动程序在其他平台上是否可用,但是确保代码
可以被轻松地移植却是很简单的。
清晰度: 做到所有人都能修补这个驱动程序将会很有好处,因为这样你将
会直接收到修复的补丁而不是 bug 报告。如果你提交一个试图
隐藏硬件工作机理的驱动程序,那么它将会被扔进废纸篓。
电源管理: 因为 Linux 正在被很多移动设备和桌面系统使用,所以你的驱
动程序也很有可能被使用在这些设备上。它应该支持最基本的电
源管理,即在需要的情况下实现系统级休眠和唤醒要用到的
.suspend 和 .resume 函数。你应该检查你的驱动程序是否能正
确地处理休眠与唤醒,如果实在无法确认,请至少把 .suspend
函数定义成返回 -ENOSYS功能未实现错误。你还应该尝试确
保你的驱动在什么都不干的情况下将耗电降到最低。要获得驱动
程序测试的指导,请参阅
Documentation/power/drivers-testing.txt。有关驱动程序电
源管理问题相对全面的概述,请参阅
Documentation/power/devices.txt。
管理: 如果一个驱动程序的作者还在进行有效的维护,那么通常除了那
些明显正确且不需要任何检查的补丁以外,其他所有的补丁都会
被转发给作者。如果你希望成为驱动程序的联系人和更新者,最
好在代码注释中写明并且在 MAINTAINERS 文件中加入这个驱动
程序的条目。
不影响设备驱动能否被接受的条件
------------------------------
供应商: 由硬件供应商来维护驱动程序通常是一件好事。不过,如果源码
树里已经有其他人提供了可稳定工作的驱动程序,那么请不要期
望“我是供应商”会成为内核改用你的驱动程序的理由。理想的情
况是:供应商与现有驱动程序的作者合作,构建一个统一完美的
驱动程序。
作者: 驱动程序是由大的 Linux 公司研发还是由你个人编写,并不影
响其是否能被内核接受。没有人对内核源码树享有特权。只要你
充分了解内核社区,你就会发现这一点。
资源列表
--------
Linux 内核主源码树:
ftp.??.kernel.org:/pub/linux/kernel/...
?? == 你的国家代码,例如 "cn"、"us"、"uk"、"fr" 等等
Linux 内核邮件列表:
linux-kernel@vger.kernel.org
[可通过向majordomo@vger.kernel.org发邮件来订阅]
Linux 设备驱动程序,第三版(探讨 2.6.10 版内核):
http://lwn.net/Kernel/LDD3/ (免费版)
LWN.net:
每周内核开发活动摘要 - http://lwn.net/
2.6 版中 API 的变更:
http://lwn.net/Articles/2.6-kernel-api/
将旧版内核的驱动程序移植到 2.6 版:
http://lwn.net/Articles/driver-porting/
KernelTrap
Linux 内核的最新动态以及开发者访谈
http://kerneltrap.org/
内核新手(KernelNewbies):
为新的内核开发者提供文档和帮助
http://kernelnewbies.org/
Linux USB项目
http://www.linux-usb.org/
写内核驱动的“不要”Arjan van de Ven著:
http://www.fenrus.org/how-to-not-write-a-device-driver-paper.pdf
内核清洁工 (Kernel Janitor):
http://janitor.kernelnewbies.org/

View file

@ -0,0 +1,416 @@
Chinese translated version of Documentation/SubmittingPatches
If you have any comment or update to the content, please contact the
original document maintainer directly. However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help. Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.
Chinese maintainer: TripleX Chung <triplex@zh-kernel.org>
---------------------------------------------------------------------
Documentation/SubmittingPatches 的中文翻译
如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
译存在问题,请联系中文版维护者。
中文版维护者: 钟宇 TripleX Chung <triplex@zh-kernel.org>
中文版翻译者: 钟宇 TripleX Chung <triplex@zh-kernel.org>
中文版校译者: 李阳 Li Yang <leo@zh-kernel.org>
王聪 Wang Cong <xiyou.wangcong@gmail.com>
以下为正文
---------------------------------------------------------------------
如何让你的改动进入内核
或者
获得亲爱的 Linus Torvalds 的关注和处理
----------------------------------
对于想要将改动提交到 Linux 内核的个人或者公司来说,如果不熟悉“规矩”,
提交的流程会让人畏惧。本文档收集了一系列建议,这些建议可以大大的提高你
的改动被接受的机会。
阅读 Documentation/SubmitChecklist 来获得在提交代码前需要检查的项目的列
表。如果你在提交一个驱动程序,那么同时阅读一下
Documentation/SubmittingDrivers 。
--------------------------
第一节 - 创建并发送你的改动
--------------------------
1) "diff -up"
-----------
使用 "diff -up" 或者 "diff -uprN" 来创建补丁。
所有内核的改动,都是以补丁的形式呈现的,补丁由 diff(1) 生成。创建补丁的
时候,要确认它是以 "unified diff" 格式创建的,这种格式由 diff(1) 的 '-u'
参数生成。而且,请使用 '-p' 参数那样会显示每个改动所在的C函数使得
产生的补丁容易读得多。补丁应该基于内核源代码树的根目录,而不是里边的任
何子目录。
为一个单独的文件创建补丁,一般来说这样做就够了:
SRCTREE= linux-2.6
MYFILE= drivers/net/mydriver.c
cd $SRCTREE
cp $MYFILE $MYFILE.orig
vi $MYFILE # make your change
cd ..
diff -up $SRCTREE/$MYFILE{.orig,} > /tmp/patch
为多个文件创建补丁,你可以解开一个没有修改过的内核源代码树,然后和你自
己的代码树之间做 diff 。例如:
MYSRC= /devel/linux-2.6
tar xvfz linux-2.6.12.tar.gz
mv linux-2.6.12 linux-2.6.12-vanilla
diff -uprN -X linux-2.6.12-vanilla/Documentation/dontdiff \
linux-2.6.12-vanilla $MYSRC > /tmp/patch
"dontdiff" 是内核在编译的时候产生的文件的列表,列表中的文件在 diff(1)
产生的补丁里会被跳过。"dontdiff" 文件被包含在2.6.12和之后版本的内核源代
码树中。对于更早的内核版本,你可以从
<http://www.xenotime.net/linux/doc/dontdiff> 获取它。
确定你的补丁里没有包含任何不属于这次补丁提交的额外文件。记得在用diff(1)
生成补丁之后,审阅一次补丁,以确保准确。
如果你的改动很散乱,你应该研究一下如何将补丁分割成独立的部分,将改动分
割成一系列合乎逻辑的步骤。这样更容易让其他内核开发者审核,如果你想你的
补丁被接受,这是很重要的。下面这些脚本能够帮助你做这件事情:
Quilt:
http://savannah.nongnu.org/projects/quilt
Andrew Morton 的补丁脚本:
http://www.zip.com.au/~akpm/linux/patches/
作为这些脚本的替代quilt 是值得推荐的补丁管理工具(看上面的链接)。
2)描述你的改动。
描述你的改动包含的技术细节。
要多具体就写多具体。最糟糕的描述可能是像下面这些语句:“更新了某驱动程
序”“修正了某驱动程序的bug”或者“这个补丁包含了某子系统的修改
使用。”
如果你的描述开始变长这表示你也许需要拆分你的补丁了请看第3小节
继续。
3)拆分你的改动
将改动拆分,逻辑类似的放到同一个补丁文件里。
例如如果你的改动里同时有bug修正和性能优化那么把这些改动才分到两个或
者更多的补丁文件中。如果你的改动包含对API的修改并且修改了驱动程序来适
应这些新的API那么把这些修改分成两个补丁。
另一方面,如果你将一个单独的改动做成多个补丁文件,那么将它们合并成一个
单独的补丁文件。这样一个逻辑上单独的改动只被包含在一个补丁文件里。
如果有一个补丁依赖另外一个补丁来完成它的改动,那没问题。简单的在你的补
丁描述里指出“这个补丁依赖某补丁”就好了。
如果你不能将补丁浓缩成更少的文件那么每次大约发送出15个然后等待审查
和整合。
4)选择 e-mail 的收件人
看一遍 MAINTAINERS 文件和源代码,看看你所的改动所在的内核子系统有没有指
定的维护者。如果有给他们发e-mail。
如果没有找到维护者,或者维护者没有反馈,将你的补丁发送到内核开发者主邮
件列表 linux-kernel@vger.kernel.org。大部分的内核开发者都跟踪这个邮件列
表,可以评价你的改动。
每次不要发送超过15个补丁到 vger 邮件列表!!!
Linus Torvalds 是决定改动能否进入 Linux 内核的最终裁决者。他的 e-mail
地址是 <torvalds@linux-foundation.org> 。他收到的 e-mail 很多,所以一般
的说,最好别给他发 e-mail。
那些修正bug“显而易见”的修改或者是类似的只需要很少讨论的补丁可以直接
发送或者CC给Linus。那些需要讨论或者没有很清楚的好处的补丁一般先发送到
linux-kernel邮件列表。只有当补丁被讨论得差不多了才提交给Linus。
5)选择CC( e-mail 抄送)列表
除非你有理由不这样做否则CC linux-kernel@vger.kernel.org。
除了 Linus 之外,其他内核开发者也需要注意到你的改动,这样他们才能评论你
的改动并提供代码审查和建议。linux-kernel 是 Linux 内核开发者主邮件列表
。其它的邮件列表为特定的子系统提供服务,比如 USBframebuffer 设备,虚
拟文件系统SCSI 子系统,等等。查看 MAINTAINERS 文件来获得和你的改动有
关的邮件列表。
Majordomo lists of VGER.KERNEL.ORG at:
<http://vger.kernel.org/vger-lists.html>
如果改动影响了用户空间和内核之间的接口,请给 MAN-PAGES 的维护者(列在
MAITAINERS 文件里的发送一个手册页man-pages补丁或者至少通知一下改
变,让一些信息有途径进入手册页。
即使在第四步的时候,维护者没有作出回应,也要确认在修改他们的代码的时候
一直将维护者拷贝到CC列表中。
对于小的补丁你也许会CC到 Adrian Bunk 管理的搜集琐碎补丁的邮件列表
(Trivial Patch Monkey)trivial@kernel.org那里专门收集琐碎的补丁。下面这样
的补丁会被看作“琐碎的”补丁:
文档的拼写修正。
修正会影响到 grep(1) 的拼写。
警告信息修正(频繁的打印无用的警告是不好的。)
编译错误修正(代码逻辑的确是对的,只是编译有问题。)
运行时修正(只要真的修正了错误。)
移除使用了被废弃的函数/宏的代码(例如 check_region。)
联系方式和文档修正。
用可移植的代码替换不可移植的代码(即使在体系结构相关的代码中,既然有
人拷贝,只要它是琐碎的)
任何文件的作者/维护者对该文件的改动(例如 patch monkey 在重传模式下)
URL: <http://www.kernel.org/pub/linux/kernel/people/bunk/trivial/>
(译注,关于“琐碎补丁”的一些说明:因为原文的这一部分写得比较简单,所以不得不
违例写一下译注。"trivial"这个英文单词的本意是“琐碎的,不重要的。”但是在这里
有稍微有一些变化例如对一些明显的NULL指针的修正属于运行时修正会被归类
到琐碎补丁里。虽然NULL指针的修正很重要但是这样的修正往往很小而且很容易得到
检验,所以也被归入琐碎补丁。琐碎补丁更精确的归类应该是
“simple, localized & easy to verify”也就是说简单的局部的和易于检验的。
trivial@kernel.org邮件列表的目的是针对这样的补丁为提交者提供一个中心
降低提交的门槛。)
6)没有 MIME 编码,没有链接,没有压缩,没有附件,只有纯文本。
Linus 和其他的内核开发者需要阅读和评论你提交的改动。对于内核开发者来说
,可以“引用”你的改动很重要,使用一般的 e-mail 工具,他们就可以在你的
代码的任何位置添加评论。
因为这个原因,所有的提交的补丁都是 e-mail 中“内嵌”的。
警告:如果你使用剪切-粘贴你的补丁,小心你的编辑器的自动换行功能破坏你的
补丁。
不要将补丁作为 MIME 编码的附件,不管是否压缩。很多流行的 e-mail 软件不
是任何时候都将 MIME 编码的附件当作纯文本发送的,这会使得别人无法在你的
代码中加评论。另外MIME 编码的附件会让 Linus 多花一点时间来处理,这就
降低了你的改动被接受的可能性。
警告:一些邮件软件,比如 Mozilla 会将你的信息以如下格式发送:
---- 邮件头 ----
Content-Type: text/plain; charset=us-ascii; format=flowed
---- 邮件头 ----
问题在于 “format=flowed” 会让接收端的某些邮件软件将邮件中的制表符替换
成空格以及做一些类似的替换。这样,你发送的时候看起来没问题的补丁就被破
坏了。
要修正这个问题,只需要将你的 mozilla 的 defaults/pref/mailnews.js 文件
里的
pref("mailnews.send_plaintext_flowed", false); // RFC 2646=======
修改成
pref("mailnews.display.disable_format_flowed_support", true);
就可以了。
7) e-mail 的大小
给 Linus 发送补丁的时候永远按照第6小节说的做。
大的改动对邮件列表不合适,对某些维护者也不合适。如果你的补丁,在不压缩
的情况下超过了40kB那么你最好将补丁放在一个能通过 internet 访问的服
务器上,然后用指向你的补丁的 URL 替代。
8) 指出你的内核版本
在标题和在补丁的描述中,指出补丁对应的内核的版本,是很重要的。
如果补丁不能干净的在最新版本的内核上打上Linus 是不会接受它的。
9) 不要气馁,继续提交。
当你提交了改动以后,耐心地等待。如果 Linus 喜欢你的改动并且同意它,那么
它将在下一个内核发布版本中出现。
然而,如果你的改动没有出现在下一个版本的内核中,可能有若干原因。减少那
些原因,修正错误,重新提交更新后的改动,是你自己的工作。
Linus不给出任何评论就“丢弃”你的补丁是常见的事情。在系统中这样的事情很
平常。如果他没有接受你的补丁,也许是由于以下原本:
* 你的补丁不能在最新版本的内核上干净的打上。
* 你的补丁在 linux-kernel 邮件列表中没有得到充分的讨论。
* 风格问题参照第2小节
* 邮件格式问题(重读本节)
* 你的改动有技术问题。
* 他收到了成吨的 e-mail而你的在混乱中丢失了。
* 你让人为难。
有疑问的时候,在 linux-kernel 邮件列表上请求评论。
10) 在标题上加上 PATCH 的字样
Linus 和 linux-kernel 邮件列表的 e-mail 流量都很高,一个通常的约定是标
题行以 [PATCH] 开头。这样可以让 Linus 和其他内核开发人员可以从 e-mail
的讨论中很轻易的将补丁分辨出来。
11为你的工作签名
为了加强对谁做了何事的追踪,尤其是对那些透过好几层的维护者的补丁,我们
建议在发送出去的补丁上加一个 “sign-off” 的过程。
"sign-off" 是在补丁的注释的最后的简单的一行文字,认证你编写了它或者其他
人有权力将它作为开放源代码的补丁传递。规则很简单:如果你能认证如下信息
开发者来源证书 1.1
对于本项目的贡献,我认证如下信息:
a这些贡献是完全或者部分的由我创建我有权利以文件中指出
的开放源代码许可证提交它;或者
b这些贡献基于以前的工作据我所知这些以前的工作受恰当的开放
源代码许可证保护,而且,根据许可证,我有权提交修改后的贡献,
无论是完全还是部分由我创造,这些贡献都使用同一个开放源代码许可证
(除非我被允许用其它的许可证),正如文件中指出的;或者
c这些贡献由认证ab或者c的人直接提供给我
且我没有修改它。
d我理解并同意这个项目和贡献是公开的贡献的记录包括我
一起提交的个人记录,包括 sign-off )被永久维护并且可以和这个项目
或者开放源代码的许可证同步地再发行。
那么加入这样一行:
Signed-off-by: Random J Developer <random@developer.example.org>
使用你的真名(抱歉,不能使用假名或者匿名。)
有人在最后加上标签。现在这些东西会被忽略,但是你可以这样做,来标记公司
内部的过程,或者只是指出关于 sign-off 的一些特殊细节。
12标准补丁格式
标准的补丁,标题行是:
Subject: [PATCH 001/123] 子系统:一句话概述
标准补丁的信体存在如下部分:
- 一个 "from" 行指出补丁作者。
- 一个空行
- 说明的主体,这些说明文字会被拷贝到描述该补丁的永久改动记录里。
- 一个由"---"构成的标记行
- 不合适放到改动记录里的额外的注解。
- 补丁本身diff 输出)
标题行的格式,使得对标题行按字母序排序非常的容易 - 很多 e-mail 客户端都
可以支持 - 因为序列号是用零填充的,所以按数字排序和按字母排序是一样的。
e-mail 标题中的“子系统”标识哪个内核子系统将被打补丁。
e-mail 标题中的“一句话概述”扼要的描述 e-mail 中的补丁。“一句话概述”
不应该是一个文件名。对于一个补丁系列(“补丁系列”指一系列的多个相关补
丁),不要对每个补丁都使用同样的“一句话概述”。
记住 e-mail 的“一句话概述”会成为该补丁的全局唯一标识。它会蔓延到 git
的改动记录里。然后“一句话概述”会被用在开发者的讨论里,用来指代这个补
丁。用户将希望通过 google 来搜索"一句话概述"来找到那些讨论这个补丁的文
章。
一些标题的例子:
Subject: [patch 2/5] ext2: improve scalability of bitmap searching
Subject: [PATCHv2 001/207] x86: fix eflags tracking
"from" 行是信体里的最上面一行,具有如下格式:
From: Original Author <author@example.com>
"from" 行指明在永久改动日志里,谁会被确认为作者。如果没有 "from" 行,那
么邮件头里的 "From: " 行会被用来决定改动日志中的作者。
说明的主题将会被提交到永久的源代码改动日志里,因此对那些早已经不记得和
这个补丁相关的讨论细节的有能力的读者来说,是有意义的。
"---" 标记行对于补丁处理工具要找到哪里是改动日志信息的结束,是不可缺少
的。
对于 "---" 标记之后的额外注解,一个好的用途就是用来写 diffstat用来显
示修改了什么文件和每个文件都增加和删除了多少行。diffstat 对于比较大的补
丁特别有用。其余那些只是和时刻或者开发者相关的注解,不合适放到永久的改
动日志里的,也应该放这里。
使用 diffstat的选项 "-p 1 -w 70" 这样文件名就会从内核源代码树的目录开始
不会占用太宽的空间很容易适合80列的宽度也许会有一些缩进。
在后面的参考资料中能看到适当的补丁格式的更多细节。
-------------------------------
第二节 提示,建议和诀窍
-------------------------------
本节包含很多和提交到内核的代码有关的通常的"规则"。事情永远有例外...但是
你必须真的有好的理由这样做。你可以把本节叫做Linus的计算机科学入门课。
1) 读 Document/CodingStyle
Nuff 说过,如果你的代码和这个偏离太多,那么它有可能会被拒绝,没有更多的
审查,没有更多的评价。
2) #ifdef 是丑陋的
混杂了 ifdef 的代码难以阅读和维护。别这样做。作为替代,将你的 ifdef 放
在头文件里,有条件地定义 "static inline" 函数,或者宏,在代码里用这些东
西。让编译器把那些"空操作"优化掉。
一个简单的例子,不好的代码:
dev = alloc_etherdev (sizeof(struct funky_private));
if (!dev)
return -ENODEV;
#ifdef CONFIG_NET_FUNKINESS
init_funky_net(dev);
#endif
清理后的例子:
(头文件里)
#ifndef CONFIG_NET_FUNKINESS
static inline void init_funky_net (struct net_device *d) {}
#endif
(代码文件里)
dev = alloc_etherdev (sizeof(struct funky_private));
if (!dev)
return -ENODEV;
init_funky_net(dev);
3) 'static inline' 比宏好
Static inline 函数相比宏来说是好得多的选择。Static inline 函数提供了
类型安全,没有长度限制,没有格式限制,在 gcc 下开销和宏一样小。
宏只在 static inline 函数不是最优的时候[在 fast paths 里有很少的独立的
案例],或者不可能用 static inline 函数的时候[例如字符串分配]。
应该用 'static inline' 而不是 'static __inline__', 'extern inline' 和
'extern __inline__' 。
4) 不要过度设计
不要试图预计模糊的未来事情,这些事情也许有用也许没有用:"让事情尽可能的
简单,而不是更简单"。
----------------
第三节 参考文献
----------------
Andrew Morton, "The perfect patch" (tpp).
<http://www.zip.com.au/~akpm/linux/patches/stuff/tpp.txt>
Jeff Garzik, "Linux kernel patch submission format".
<http://linux.yyz.us/patch-format.html>
Greg Kroah-Hartman, "How to piss off a kernel subsystem maintainer".
<http://www.kroah.com/log/2005/03/31/>
<http://www.kroah.com/log/2005/07/08/>
<http://www.kroah.com/log/2005/10/19/>
<http://www.kroah.com/log/2006/01/11/>
NO!!!! No more huge patch bombs to linux-kernel@vger.kernel.org people!
<http://marc.theaimsgroup.com/?l=linux-kernel&m=112112749912944&w=2>
Kernel Documentation/CodingStyle:
<http://sosdg.org/~coywolf/lxr/source/Documentation/CodingStyle>
Linus Torvalds's mail on the canonical patch format:
<http://lkml.org/lkml/2005/4/7/183>
--

View file

@ -0,0 +1,212 @@
Chinese translated version of Documentation/oops-tracing.txt
If you have any comment or update to the content, please contact the
original document maintainer directly. However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help. Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.
Chinese maintainer: Dave Young <hidave.darkstar@gmail.com>
---------------------------------------------------------------------
Documentation/oops-tracing.txt 的中文翻译
如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
译存在问题,请联系中文版维护者。
中文版维护者: 杨瑞 Dave Young <hidave.darkstar@gmail.com>
中文版翻译者: 杨瑞 Dave Young <hidave.darkstar@gmail.com>
中文版校译者: 李阳 Li Yang <leo@zh-kernel.org>
王聪 Wang Cong <xiyou.wangcong@gmail.com>
以下为正文
---------------------------------------------------------------------
注意: ksymoops 在2.6中是没有用的。 请以原有格式使用Oops(来自dmesg等等)。
忽略任何这样那样关于“解码Oops”或者“通过ksymoops运行”的文档。 如果你贴出运行过
ksymoops的来自2.6的Oops人们只会让你重贴一次。
快速总结
-------------
发现Oops并发送给看似相关的内核领域的维护者。别太担心对不上号。如果你不确定就发给
和你所做的事情相关的代码的负责人。 如果可重现试着描述怎样重构。 那甚至比oops更有
价值。
如果你对于发送给谁一无所知, 发给linux-kernel@vger.kernel.org。感谢你帮助Linux
尽可能地稳定。
Oops在哪里?
----------------------
通常Oops文本由klogd从内核缓冲区里读取并传给syslogd由syslogd写到syslog文件中
典型地是/var/log/messages(依赖于/etc/syslog.conf)。有时klogd崩溃了,这种情况下你
能够运行dmesg > file来从内核缓冲区中读取数据并保存下来。 否则你可以
cat /proc/kmsg > file 然而你必须介入中止传输, kmsg是一个“永不结束的文件”。如
果机器崩溃坏到你不能输入命令或者磁盘不可用那么你有三种选择:-
1 手抄屏幕上的文本待机器重启后再输入计算机。 麻烦但如果没有针对崩溃的准备,
这是仅有的选择。 另外,你可以用数码相机把屏幕拍下来-不太好,但比没有强。 如果信
息滚动到了终端的上面你会发现以高分辩率启动比如vga=791会让你读到更多的文
本。注意这需要vesafb所以对早期的oops没有帮助
2用串口终端启动请参看Documentation/serial-console.txt运行一个null
modem到另一台机器并用你喜欢的通讯工具获取输出。Minicom工作地很好。
3使用Kdump请参看Documentation/kdump/kdump.txt
使用在Documentation/kdump/gdbmacros.txt中定义的dmesg gdb宏从旧的内存中提取内核
环形缓冲区。
完整信息
----------------
注意以下来自于Linus的邮件适用于2.4内核。 我因为历史原因保留了它,并且因为其中
一些信息仍然适用。 特别注意的是请忽略任何ksymoops的引用。
From: Linus Torvalds <torvalds@osdl.org>
怎样跟踪Oops.. [原发到linux-kernel的一封邮件]
主要的窍门是有五年和这些烦人的oops消息打交道的经验;-)
实际上,你有办法使它更简单。我有两个不同的方法:
gdb /usr/src/linux/vmlinux
gdb> disassemble <offending_function>
那是发现问题的简单办法至少如果bug报告做的好的情况下象这个一样-运行ksymoops
得到oops发生的函数及函数内的偏移
哦,如果报告发生的内核以相同的编译器和相似的配置编译它会有帮助的。
另一件要做的事是反汇编bug报告的“Code”部分ksymoops也会用正确的工具来做这件事
但如果没有那些工具你可以写一个傻程序:
char str[] = "\xXX\xXX\xXX...";
main(){}
并用gcc -g编译它然后执行“disassemble str”XX部分是由Oops报告的值-你可以仅剪切
粘贴并用“\x”替换空格-我就是这么做的,因为我懒得写程序自动做这一切)。
另外你可以用scripts/decodecode这个shell脚本。它的使用方法是
decodecode < oops.txt
“Code”之后的十六进制字节可能在某些架构上有一些当前指令之前的指令字节以及
当前和之后的指令字节
Code: f9 0f 8d f9 00 00 00 8d 42 0c e8 dd 26 11 c7 a1 60 ea 2b f9 8b 50 08 a1
64 ea 2b f9 8d 34 82 8b 1e 85 db 74 6d 8b 15 60 ea 2b f9 <8b> 43 04 39 42 54
7e 04 40 89 42 54 8b 43 04 3b 05 00 f6 52 c0
最后,如果你想知道代码来自哪里,你可以:
cd /usr/src/linux
make fs/buffer.s # 或任何产生BUG的文件
然后你会比gdb反汇编更清楚的知道发生了什么。
现在问题是把你所拥有的所有数据结合起来C源码关于它应该怎样的一般知识
汇编代码及其反汇编得到的代码另外还有从“oops”消息得到的寄存器状态-对了解毁坏的
指针有用而且当你有了汇编代码你也能拿其它的寄存器和任何它们对应的C表达式做匹配
)。
实际上你仅需看看哪里不匹配这个例子是“Code”反汇编和编译器生成的代码不匹配
然后你须要找出为什么不匹配。通常很简单-你看到代码使用了空指针然后你看代码想知道
空指针是怎么出现的,还有检查它是否合法..
现在,如果明白这是一项耗时的工作而且需要一丁点儿的专心,没错。这就是我为什么大多
只是忽略那些没有符号表信息的崩溃报告的原因:简单的说太难查找了(我有一些
程序用于在内核代码段中搜索特定的模式,而且有时我也已经能找出那些崩溃的地方,但是
仅仅是找出正确的序列也确实需要相当扎实的内核知识)
_有时_会发生这种情况我仅看到崩溃中的反汇编代码序列 然后我马上就明白问题出在
哪里。这时我才意识到自己干这个工作已经太长时间了;-)
Linus
---------------------------------------------------------------------------
关于Oops跟踪的注解
为了帮助Linus和其它内核开发者klogd纳入了大量的支持来处理保护错误。为了拥有对
地址解析的完整支持至少应该使用1.3-pl3的sysklogd包。
当保护错误发生时klogd守护进程自动把内核日志信息中的重要地址翻译成它们相应的符
号。
klogd执行两种类型的地址解析。首先是静态翻译其次是动态翻译。静态翻译和ksymoops
一样使用System.map文件。为了做静态翻译klogd守护进程必须在初始化时能找到system
map文件。关于klogd怎样搜索map文件请参看klogd手册页。
动态地址翻译在使用内核可装载模块时很重要。 因为内核模块的内存是从内核动态内存池
里分配的,所以不管是模块开始位置还是模块中函数和符号的位置都不是固定的。
内核支持允许程序决定装载哪些模块和它们在内存中位置的系统调用。使用这些系统调用
klogd守护进程生成一张符号表用于调试发生在可装载模块中的保护错误。
至少klogd会提供产生保护错误的模块名。还可有额外的符号信息供可装载模块开发者选择
以从模块中输出符号信息。
因为内核模块环境可能是动态的所以必须有一种机制当模块环境发生改变时来通知klogd
守护进程。 有一些可用的命令行选项允许klogd向当前执行中的守护进程发送信号告知符
号信息应该被刷新了。 更多信息请参看klogd手册页。
sysklogd发布时包含一个补丁修改了modules-2.0.0包,无论何时一个模块装载或者卸载都
会自动向klogd发送信号。打上这个补丁提供了必要的对调试发生于内核可装载模块的保护
错误的无缝支持。
以下是被klogd处理过的发生在可装载模块中的一个保护错误例子
---------------------------------------------------------------------------
Aug 29 09:51:01 blizard kernel: Unable to handle kernel paging request at virtual address f15e97cc
Aug 29 09:51:01 blizard kernel: current->tss.cr3 = 0062d000, %cr3 = 0062d000
Aug 29 09:51:01 blizard kernel: *pde = 00000000
Aug 29 09:51:01 blizard kernel: Oops: 0002
Aug 29 09:51:01 blizard kernel: CPU: 0
Aug 29 09:51:01 blizard kernel: EIP: 0010:[oops:_oops+16/3868]
Aug 29 09:51:01 blizard kernel: EFLAGS: 00010212
Aug 29 09:51:01 blizard kernel: eax: 315e97cc ebx: 003a6f80 ecx: 001be77b edx: 00237c0c
Aug 29 09:51:01 blizard kernel: esi: 00000000 edi: bffffdb3 ebp: 00589f90 esp: 00589f8c
Aug 29 09:51:01 blizard kernel: ds: 0018 es: 0018 fs: 002b gs: 002b ss: 0018
Aug 29 09:51:01 blizard kernel: Process oops_test (pid: 3374, process nr: 21, stackpage=00589000)
Aug 29 09:51:01 blizard kernel: Stack: 315e97cc 00589f98 0100b0b4 bffffed4 0012e38e 00240c64 003a6f80 00000001
Aug 29 09:51:01 blizard kernel: 00000000 00237810 bfffff00 0010a7fa 00000003 00000001 00000000 bfffff00
Aug 29 09:51:01 blizard kernel: bffffdb3 bffffed4 ffffffda 0000002b 0007002b 0000002b 0000002b 00000036
Aug 29 09:51:01 blizard kernel: Call Trace: [oops:_oops_ioctl+48/80] [_sys_ioctl+254/272] [_system_call+82/128]
Aug 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
---------------------------------------------------------------------------
Dr. G.W. Wettstein Oncology Research Div. Computing Facility
Roger Maris Cancer Center INTERNET: greg@wind.rmcc.com
820 4th St. N.
Fargo, ND 58122
Phone: 701-234-7556
---------------------------------------------------------------------------
受污染的内核
一些oops报告在程序记数器之后包含字符串'Tainted: '。这表明内核已经被一些东西给污
染了。 该字符串之后紧跟着一系列的位置敏感的字符,每个代表一个特定的污染值。
1'G'如果所有装载的模块都有GPL或相容的许可证'P'如果装载了任何的专有模块。
没有模块MODULE_LICENSE或者带有insmod认为是与GPL不相容的的MODULE_LICENSE的模块被
认定是专有的。
2'F'如果有任何通过“insmod -f”被强制装载的模块' '如果所有模块都被正常装载。
3'S'如果oops发生在SMP内核中运行于没有证明安全运行多处理器的硬件。 当前这种
情况仅限于几种不支持SMP的速龙处理器。
4'R'如果模块通过“insmod -f”被强制装载' '如果所有模块都被正常装载。
5'M'如果任何处理器报告了机器检查异常,' '如果没有发生机器检查异常。
6'B'如果页释放函数发现了一个错误的页引用或者一些非预期的页标志。
7'U'如果用户或者用户应用程序特别请求设置污染标志,否则' '。
8'D'如果内核刚刚死掉比如有OOPS或者BUG。
使用'Tainted: '字符串的主要原因是要告诉内核调试者,这是否是一个干净的内核亦或发
生了任何的不正常的事。污染是永久的:即使出错的模块已经被卸载了,污染值仍然存在,
以表明内核不再值得信任。

View file

@ -0,0 +1,100 @@
Chinese translated version of Documentation/sparse.txt
If you have any comment or update to the content, please contact the
original document maintainer directly. However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help. Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.
Chinese maintainer: Li Yang <leo@zh-kernel.org>
---------------------------------------------------------------------
Documentation/sparse.txt 的中文翻译
如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
译存在问题,请联系中文版维护者。
中文版维护者: 李阳 Li Yang <leo@zh-kernel.org>
中文版翻译者: 李阳 Li Yang <leo@zh-kernel.org>
以下为正文
---------------------------------------------------------------------
Copyright 2004 Linus Torvalds
Copyright 2004 Pavel Machek <pavel@suse.cz>
Copyright 2006 Bob Copeland <me@bobcopeland.com>
使用 sparse 工具做类型检查
~~~~~~~~~~~~~~~~~~~~~~~~~~
"__bitwise" 是一种类型属性,所以你应该这样使用它:
typedef int __bitwise pm_request_t;
enum pm_request {
PM_SUSPEND = (__force pm_request_t) 1,
PM_RESUME = (__force pm_request_t) 2
};
这样会使 PM_SUSPEND 和 PM_RESUME 成为位方式(bitwise)整数(使用"__force"
是因为 sparse 会抱怨改变位方式的类型转换,但是这里我们确实需要强制进行转
换)。而且因为所有枚举值都使用了相同的类型,这里的"enum pm_request"也将
会使用那个类型做为底层实现。
而且使用 gcc 编译的时候,所有的 __bitwise/__force 都会消失,最后在 gcc
看来它们只不过是普通的整数。
坦白来说,你并不需要使用枚举类型。上面那些实际都可以浓缩成一个特殊的"int
__bitwise"类型。
所以更简单的办法只要这样做:
typedef int __bitwise pm_request_t;
#define PM_SUSPEND ((__force pm_request_t) 1)
#define PM_RESUME ((__force pm_request_t) 2)
现在你就有了严格的类型检查所需要的所有基础架构。
一个小提醒:常数整数"0"是特殊的。你可以直接把常数零当作位方式整数使用而
不用担心 sparse 会抱怨。这是因为"bitwise"(恰如其名)是用来确保不同位方
式类型不会被弄混小尾模式大尾模式cpu尾模式或者其他对他们来说
常数"0"确实是特殊的。
获取 sparse 工具
~~~~~~~~~~~~~~~~
你可以从 Sparse 的主页获取最新的发布版本:
http://www.kernel.org/pub/linux/kernel/people/josh/sparse/
或者,你也可以使用 git 克隆最新的 sparse 开发版本:
git://git.kernel.org/pub/scm/linux/kernel/git/josh/sparse.git
DaveJ 把每小时自动生成的 git 源码树 tar 包放在以下地址:
http://www.codemonkey.org.uk/projects/git-snapshots/sparse/
一旦你下载了源码,只要以普通用户身份运行:
make
make install
它将会被自动安装到你的 ~/bin 目录下。
使用 sparse 工具
~~~~~~~~~~~~~~~~
用"make C=1"命令来编译内核,会对所有重新编译的 C 文件使用 sparse 工具。
或者使用"make C=2"命令,无论文件是否被重新编译都会对其使用 sparse 工具。
如果你已经编译了内核,用后一种方式可以很快地检查整个源码树。
make 的可选变量 CHECKFLAGS 可以用来向 sparse 工具传递参数。编译系统会自
动向 sparse 工具传递 -Wbitwise 参数。你可以定义 __CHECK_ENDIAN__ 来进行
大小尾检查。
make C=2 CHECKFLAGS="-D__CHECK_ENDIAN__"
这些检查默认都是被关闭的,因为他们通常会产生大量的警告。

View file

@ -0,0 +1,66 @@
Chinese translated version of Documentation/stable_kernel_rules.txt
If you have any comment or update to the content, please contact the
original document maintainer directly. However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help. Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.
Chinese maintainer: TripleX Chung <triplex@zh-kernel.org>
---------------------------------------------------------------------
Documentation/stable_kernel_rules.txt 的中文翻译
如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
译存在问题,请联系中文版维护者。
中文版维护者: 钟宇 TripleX Chung <triplex@zh-kernel.org>
中文版翻译者: 钟宇 TripleX Chung <triplex@zh-kernel.org>
中文版校译者: 李阳 Li Yang <leo@zh-kernel.org>
Kangkai Yin <e12051@motorola.com>
以下为正文
---------------------------------------------------------------------
关于Linux 2.6稳定版发布,所有你想知道的事情。
关于哪些类型的补丁可以被接收进入稳定版代码树,哪些不可以的规则:
- 必须是显而易见的正确,并且经过测试的。
- 连同上下文不能大于100行。
- 必须只修正一件事情。
- 必须修正了一个给大家带来麻烦的真正的bug不是“这也许是一个问题...”
那样的东西)。
- 必须修正带来如下后果的问题编译错误对被标记为CONFIG_BROKEN的例外
内核崩溃,挂起,数据损坏,真正的安全问题,或者一些类似“哦,这不
好”的问题。简短的说,就是一些致命的问题。
- 没有“理论上的竞争条件”,除非能给出竞争条件如何被利用的解释。
- 不能存在任何的“琐碎的”修正(拼写修正,去掉多余空格之类的)。
- 必须被相关子系统的维护者接受。
- 必须遵循Documentation/SubmittingPatches里的规则。
向稳定版代码树提交补丁的过程:
- 在确认了补丁符合以上的规则后将补丁发送到stable@kernel.org。
- 如果补丁被接受到队列里发送者会收到一个ACK回复如果没有被接受
到的是NAK回复。回复需要几天的时间这取决于开发者的时间安排。
- 被接受的补丁会被加到稳定版本队列里,等待其他开发者的审查。
- 安全方面的补丁不要发到这个列表应该发送到security@kernel.org。
审查周期:
- 当稳定版的维护者决定开始一个审查周期,补丁将被发送到审查委员会,以
及被补丁影响的领域的维护者(除非提交者就是该领域的维护者)并且抄送
到linux-kernel邮件列表。
- 审查委员会有48小时的时间用来决定给该补丁回复ACK还是NAK。
- 如果委员会中有成员拒绝这个补丁或者linux-kernel列表上有人反对这个
补丁,并提出维护者和审查委员会之前没有意识到的问题,补丁会从队列中
丢弃。
- 在审查周期结束的时候那些得到ACK回应的补丁将会被加入到最新的稳定版
发布中,一个新的稳定版发布就此产生。
- 安全性补丁将从内核安全小组那里直接接收到稳定版代码树中,而不是通过
通常的审查周期。请联系内核安全小组以获得关于这个过程的更多细节。
审查委员会:
- 由一些自愿承担这项任务的内核开发者,和几个非志愿的组成。

View file

@ -0,0 +1,113 @@
Chinese translated version of Documentation/volatile-considered-harmful.txt
If you have any comment or update to the content, please contact the
original document maintainer directly. However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help. Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.
Maintainer: Jonathan Corbet <corbet@lwn.net>
Chinese maintainer: Bryan Wu <bryan.wu@analog.com>
---------------------------------------------------------------------
Documentation/volatile-considered-harmful.txt 的中文翻译
如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
译存在问题,请联系中文版维护者。
英文版维护者: Jonathan Corbet <corbet@lwn.net>
中文版维护者: 伍鹏 Bryan Wu <bryan.wu@analog.com>
中文版翻译者: 伍鹏 Bryan Wu <bryan.wu@analog.com>
中文版校译者: 张汉辉 Eugene Teo <eugeneteo@kernel.sg>
杨瑞 Dave Young <hidave.darkstar@gmail.com>
以下为正文
---------------------------------------------------------------------
为什么不应该使用“volatile”类型
------------------------------
C程序员通常认为volatile表示某个变量可以在当前执行的线程之外被改变因此在内核
中用到共享数据结构时常常会有C程序员喜欢使用volatile这类变量。换句话说他们经
常会把volatile类型看成某种简易的原子变量当然它们不是。在内核中使用volatile几
乎总是错误的;本文档将解释为什么这样。
理解volatile的关键是知道它的目的是用来消除优化实际上很少有人真正需要这样的应
用。在内核中,程序员必须防止意外的并发访问破坏共享的数据结构,这其实是一个完全
不同的任务。用来防止意外并发访问的保护措施,可以更加高效的避免大多数优化相关的
问题。
像volatile一样内核提供了很多原语来保证并发访问时的数据安全自旋锁, 互斥量,内
存屏障等等),同样可以防止意外的优化。如果可以正确使用这些内核原语,那么就没有
必要再使用volatile。如果仍然必须使用volatile那么几乎可以肯定在代码的某处有一
个bug。在正确设计的内核代码中volatile能带来的仅仅是使事情变慢。
思考一下这段典型的内核代码:
spin_lock(&the_lock);
do_something_on(&shared_data);
do_something_else_with(&shared_data);
spin_unlock(&the_lock);
如果所有的代码都遵循加锁规则当持有the_lock的时候不可能意外的改变shared_data的
值。任何可能访问该数据的其他代码都会在这个锁上等待。自旋锁原语跟内存屏障一样—— 它
们显式的用来书写成这样 —— 意味着数据访问不会跨越它们而被优化。所以本来编译器认为
它知道在shared_data里面将有什么但是因为spin_lock()调用跟内存屏障一样,会强制编
译器忘记它所知道的一切。那么在访问这些数据时不会有优化的问题。
如果shared_data被声名为volatile锁操作将仍然是必须的。就算我们知道没有其他人正在
使用它编译器也将被阻止优化对临界区内shared_data的访问。在锁有效的同时
shared_data不是volatile的。在处理共享数据的时候适当的锁操作可以不再需要
volatile —— 并且是有潜在危害的。
volatile的存储类型最初是为那些内存映射的I/O寄存器而定义。在内核里寄存器访问也应
该被锁保护但是人们也不希望编译器“优化”临界区内的寄存器访问。内核里I/O的内存访问
是通过访问函数完成的不赞成通过指针对I/O内存的直接访问并且不是在所有体系架构上
都能工作。那些访问函数正是为了防止意外优化而写的因此再说一次volatile类型不
是必需的。
另一种引起用户可能使用volatile的情况是当处理器正忙着等待一个变量的值。正确执行一
个忙等待的方法是:
while (my_variable != what_i_want)
cpu_relax();
cpu_relax()调用会降低CPU的能量消耗或者让位于超线程双处理器它也作为内存屏障一样出
所以再一次volatile不是必需的。当然忙等待一开始就是一种反常规的做法。
在内核中一些稀少的情况下volatile仍然是有意义的
- 在一些体系架构的系统上允许直接的I/0内存访问那么前面提到的访问函数可以使用
volatile。基本上每一个访问函数调用它自己都是一个小的临界区域并且保证了按照
程序员期望的那样发生访问操作。
- 某些会改变内存的内联汇编代码虽然没有什么其他明显的附作用但是有被GCC删除的可
能性。在汇编声明中加上volatile关键字可以防止这种删除操作。
- Jiffies变量是一种特殊情况虽然每次引用它的时候都可以有不同的值但读jiffies
变量时不需要任何特殊的加锁保护。所以jiffies变量可以使用volatile但是不赞成
其他跟jiffies相同类型变量使用volatile。Jiffies被认为是一种“愚蠢的遗留物"
Linus的话因为解决这个问题比保持现状要麻烦的多。
- 由于某些I/0设备可能会修改连续一致的内存,所以有时,指向连续一致内存的数据结构
的指针需要正确的使用volatile。网络适配器使用的环状缓存区正是这类情形的一个例
子,其中适配器用改变指针来表示哪些描述符已经处理过了。
对于大多代码上述几种可以使用volatile的情况都不适用。所以使用volatile是一种
bug并且需要对这样的代码额外仔细检查。那些试图使用volatile的开发人员需要退一步想想
他们真正想实现的是什么。
非常欢迎删除volatile变量的补丁 只要证明这些补丁完整的考虑了并发问题。
注释
----
[1] http://lwn.net/Articles/233481/
[2] http://lwn.net/Articles/233482/
致谢
----
最初由Randy Dunlap推动并作初步研究
由Jonathan Corbet撰写
参考Satyam SharmaJohannes StezenbachJesper JuhlHeikki Orsila
H. Peter AnvinPhilipp Hahn和Stefan Richter的意见改善了本档。

View file

@ -195,7 +195,7 @@ static int leds_shutdown(struct sys_device *dev)
}
static struct sysdev_class leds_sysclass = {
set_kset_name("leds"),
.name = "leds",
.shutdown = leds_shutdown,
.suspend = leds_suspend,
.resume = leds_resume,
@ -369,7 +369,7 @@ static int timer_resume(struct sys_device *dev)
#endif
static struct sysdev_class timer_sysclass = {
set_kset_name("timer"),
.name = "timer",
.suspend = timer_suspend,
.resume = timer_resume,
};

View file

@ -214,7 +214,7 @@ static int irq_resume(struct sys_device *dev)
#endif
static struct sysdev_class irq_class = {
set_kset_name("irq"),
.name = "irq",
.suspend = irq_suspend,
.resume = irq_resume,
};

View file

@ -69,14 +69,14 @@ static unsigned int mpui1610_sleep_save[MPUI1610_SLEEP_SAVE_SIZE];
static unsigned short enable_dyn_sleep = 1;
static ssize_t omap_pm_sleep_while_idle_show(struct kset *kset, char *buf)
static ssize_t idle_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
return sprintf(buf, "%hu\n", enable_dyn_sleep);
}
static ssize_t omap_pm_sleep_while_idle_store(struct kset *kset,
const char * buf,
size_t n)
static ssize_t idle_store(struct kobject *kobj, struct kobj_attribute *attr,
const char * buf, size_t n)
{
unsigned short value;
if (sscanf(buf, "%hu", &value) != 1 ||
@ -88,16 +88,9 @@ static ssize_t omap_pm_sleep_while_idle_store(struct kset *kset,
return n;
}
static struct subsys_attribute sleep_while_idle_attr = {
.attr = {
.name = __stringify(sleep_while_idle),
.mode = 0644,
},
.show = omap_pm_sleep_while_idle_show,
.store = omap_pm_sleep_while_idle_store,
};
static struct kobj_attribute sleep_while_idle_attr =
__ATTR(sleep_while_idle, 0644, idle_show, idle_store);
extern struct kset power_subsys;
static void (*omap_sram_idle)(void) = NULL;
static void (*omap_sram_suspend)(unsigned long r0, unsigned long r1) = NULL;
@ -726,9 +719,9 @@ static int __init omap_pm_init(void)
omap_pm_init_proc();
#endif
error = subsys_create_file(&power_subsys, &sleep_while_idle_attr);
error = sysfs_create_file(power_kobj, &sleep_while_idle_attr);
if (error)
printk(KERN_ERR "subsys_create_file failed: %d\n", error);
printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
if (cpu_is_omap16xx()) {
/* configure LOW_PWR pin */

View file

@ -566,7 +566,7 @@ static int cmx270_resume(struct sys_device *dev)
}
static struct sysdev_class cmx270_pm_sysclass = {
set_kset_name("pm"),
.name = "pm",
.resume = cmx270_resume,
.suspend = cmx270_suspend,
};

View file

@ -122,7 +122,7 @@ static int lpd270_irq_resume(struct sys_device *dev)
}
static struct sysdev_class lpd270_irq_sysclass = {
set_kset_name("cpld_irq"),
.name = "cpld_irq",
.resume = lpd270_irq_resume,
};

View file

@ -126,7 +126,7 @@ static int lubbock_irq_resume(struct sys_device *dev)
}
static struct sysdev_class lubbock_irq_sysclass = {
set_kset_name("cpld_irq"),
.name = "cpld_irq",
.resume = lubbock_irq_resume,
};

View file

@ -120,7 +120,7 @@ static int mainstone_irq_resume(struct sys_device *dev)
}
static struct sysdev_class mainstone_irq_sysclass = {
set_kset_name("cpld_irq"),
.name = "cpld_irq",
.resume = mainstone_irq_resume,
};

View file

@ -100,7 +100,7 @@ void __init s3c2410_init_clocks(int xtal)
}
struct sysdev_class s3c2410_sysclass = {
set_kset_name("s3c2410-core"),
.name = "s3c2410-core",
};
static struct sys_device s3c2410_sysdev = {

View file

@ -196,7 +196,7 @@ void __init s3c2412_init_clocks(int xtal)
*/
struct sysdev_class s3c2412_sysclass = {
set_kset_name("s3c2412-core"),
.name = "s3c2412-core",
};
static int __init s3c2412_core_init(void)

View file

@ -312,7 +312,7 @@ static int osiris_pm_resume(struct sys_device *sd)
#endif
static struct sysdev_class osiris_pm_sysclass = {
set_kset_name("mach-osiris"),
.name = "mach-osiris",
.suspend = osiris_pm_suspend,
.resume = osiris_pm_resume,
};

View file

@ -43,7 +43,7 @@ static struct map_desc s3c2443_iodesc[] __initdata = {
};
struct sysdev_class s3c2443_sysclass = {
set_kset_name("s3c2443-core"),
.name = "s3c2443-core",
};
static struct sys_device s3c2443_sysdev = {

View file

@ -283,7 +283,7 @@ static int sa1100irq_resume(struct sys_device *dev)
}
static struct sysdev_class sa1100irq_sysclass = {
set_kset_name("sa11x0-irq"),
.name = "sa11x0-irq",
.suspend = sa1100irq_suspend,
.resume = sa1100irq_resume,
};

View file

@ -96,7 +96,7 @@ static int op_arm_resume(struct sys_device *dev)
}
static struct sysdev_class oprofile_sysclass = {
set_kset_name("oprofile"),
.name = "oprofile",
.resume = op_arm_resume,
.suspend = op_arm_suspend,
};

View file

@ -1455,7 +1455,7 @@ static int omap_gpio_resume(struct sys_device *dev)
}
static struct sysdev_class omap_gpio_sysclass = {
set_kset_name("gpio"),
.name = "gpio",
.suspend = omap_gpio_suspend,
.resume = omap_gpio_resume,
};

View file

@ -1265,7 +1265,7 @@ static int s3c2410_dma_resume(struct sys_device *dev)
#endif /* CONFIG_PM */
struct sysdev_class dma_sysclass = {
set_kset_name("s3c24xx-dma"),
.name = "s3c24xx-dma",
.suspend = s3c2410_dma_suspend,
.resume = s3c2410_dma_resume,
};

View file

@ -151,13 +151,13 @@ static int s3c244x_resume(struct sys_device *dev)
/* Since the S3C2442 and S3C2440 share items, put both sysclasses here */
struct sysdev_class s3c2440_sysclass = {
set_kset_name("s3c2440-core"),
.name = "s3c2440-core",
.suspend = s3c244x_suspend,
.resume = s3c244x_resume
};
struct sysdev_class s3c2442_sysclass = {
set_kset_name("s3c2442-core"),
.name = "s3c2442-core",
.suspend = s3c244x_suspend,
.resume = s3c244x_resume
};

View file

@ -214,7 +214,7 @@ void __init time_init(void)
}
static struct sysdev_class timer_class = {
set_kset_name("timer"),
.name = "timer",
};
static struct sys_device timer_device = {

View file

@ -20,6 +20,9 @@
#define IOP_TIMEOUT 100
#error "This driver is broken with regard to its driver core usage."
#error "Please contact <greg@kroah.com> for details on how to fix it properly."
static struct device iop_spu_device[2] = {
{ .bus_id = "iop-spu0", },
{ .bus_id = "iop-spu1", },
@ -192,6 +195,13 @@ int iop_start_mpu(unsigned int start_addr)
static int __init iop_fw_load_init(void)
{
#if 0
/*
* static struct devices can not be added directly to sysfs by ignoring
* the driver model infrastructure. To fix this properly, please use
* the platform_bus to register these devices to be able to properly
* use the firmware infrastructure.
*/
device_initialize(&iop_spu_device[0]);
kobject_set_name(&iop_spu_device[0].kobj, "iop-spu0");
kobject_add(&iop_spu_device[0].kobj);
@ -201,6 +211,7 @@ static int __init iop_fw_load_init(void)
device_initialize(&iop_mpu_device);
kobject_set_name(&iop_mpu_device.kobj, "iop-mpu");
kobject_add(&iop_mpu_device.kobj);
#endif
return 0;
}

View file

@ -354,27 +354,27 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
if (unlikely(retval < 0))
return retval;
all_cpu_cache_info[cpu].kobj.parent = &sys_dev->kobj;
kobject_set_name(&all_cpu_cache_info[cpu].kobj, "%s", "cache");
all_cpu_cache_info[cpu].kobj.ktype = &cache_ktype_percpu_entry;
retval = kobject_register(&all_cpu_cache_info[cpu].kobj);
retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
&cache_ktype_percpu_entry, &sys_dev->kobj,
"%s", "cache");
for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) {
this_object = LEAF_KOBJECT_PTR(cpu,i);
this_object->kobj.parent = &all_cpu_cache_info[cpu].kobj;
kobject_set_name(&(this_object->kobj), "index%1lu", i);
this_object->kobj.ktype = &cache_ktype;
retval = kobject_register(&(this_object->kobj));
retval = kobject_init_and_add(&(this_object->kobj),
&cache_ktype,
&all_cpu_cache_info[cpu].kobj,
"index%1lu", i);
if (unlikely(retval)) {
for (j = 0; j < i; j++) {
kobject_unregister(
&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
}
kobject_unregister(&all_cpu_cache_info[cpu].kobj);
kobject_put(&all_cpu_cache_info[cpu].kobj);
cpu_cache_sysfs_exit(cpu);
break;
}
kobject_uevent(&(this_object->kobj), KOBJ_ADD);
}
kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
return retval;
}
@ -385,10 +385,10 @@ static int __cpuinit cache_remove_dev(struct sys_device * sys_dev)
unsigned long i;
for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++)
kobject_unregister(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
if (all_cpu_cache_info[cpu].kobj.parent) {
kobject_unregister(&all_cpu_cache_info[cpu].kobj);
kobject_put(&all_cpu_cache_info[cpu].kobj);
memset(&all_cpu_cache_info[cpu].kobj,
0,
sizeof(struct kobject));

View file

@ -238,7 +238,7 @@ static int i8259A_shutdown(struct sys_device *dev)
}
static struct sysdev_class i8259_sysdev_class = {
set_kset_name("i8259"),
.name = "i8259",
.resume = i8259A_resume,
.shutdown = i8259A_shutdown,
};

View file

@ -459,7 +459,7 @@ static int spu_shutdown(struct sys_device *sysdev)
}
static struct sysdev_class spu_sysdev_class = {
set_kset_name("spu"),
.name = "spu",
.shutdown = spu_shutdown,
};

View file

@ -663,7 +663,7 @@ static int pmacpic_resume(struct sys_device *sysdev)
#endif /* CONFIG_PM && CONFIG_PPC32 */
static struct sysdev_class pmacpic_sysclass = {
set_kset_name("pmac_pic"),
.name = "pmac_pic",
};
static struct sys_device device_pmacpic = {

View file

@ -28,13 +28,15 @@
unsigned long rtas_poweron_auto; /* default and normal state is 0 */
static ssize_t auto_poweron_show(struct kset *kset, char *buf)
static ssize_t auto_poweron_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", rtas_poweron_auto);
}
static ssize_t
auto_poweron_store(struct kset *kset, const char *buf, size_t n)
static ssize_t auto_poweron_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t n)
{
int ret;
unsigned long ups_restart;
@ -47,17 +49,11 @@ auto_poweron_store(struct kset *kset, const char *buf, size_t n)
return -EINVAL;
}
static struct subsys_attribute auto_poweron_attr = {
.attr = {
.name = __stringify(auto_poweron),
.mode = 0644,
},
.show = auto_poweron_show,
.store = auto_poweron_store,
};
static struct kobj_attribute auto_poweron_attr =
__ATTR(auto_poweron, 0644, auto_poweron_show, auto_poweron_store);
#ifndef CONFIG_PM
decl_subsys(power,NULL,NULL);
struct kobject *power_kobj;
static struct attribute *g[] = {
&auto_poweron_attr.attr,
@ -70,18 +66,16 @@ static struct attribute_group attr_group = {
static int __init pm_init(void)
{
int error = subsystem_register(&power_subsys);
if (!error)
error = sysfs_create_group(&power_subsys.kobj, &attr_group);
return error;
power_kobj = kobject_create_and_add("power", NULL);
if (!power_kobj)
return -ENOMEM;
return sysfs_create_group(power_kobj, &attr_group);
}
core_initcall(pm_init);
#else
extern struct kset power_subsys;
static int __init apo_pm_init(void)
{
return (subsys_create_file(&power_subsys, &auto_poweron_attr));
return (sysfs_create_file(power_kobj, &auto_poweron_attr));
}
__initcall(apo_pm_init);
#endif

View file

@ -725,7 +725,7 @@ unsigned int ipic_get_irq(void)
}
static struct sysdev_class ipic_sysclass = {
set_kset_name("ipic"),
.name = "ipic",
};
static struct sys_device device_ipic = {

View file

@ -1584,7 +1584,7 @@ static struct sysdev_class mpic_sysclass = {
.resume = mpic_resume,
.suspend = mpic_suspend,
#endif
set_kset_name("mpic"),
.name = "mpic",
};
static int mpic_init_sys(void)

View file

@ -483,7 +483,7 @@ int qe_ic_set_high_priority(unsigned int virq, unsigned int priority, int high)
}
static struct sysdev_class qe_ic_sysclass = {
set_kset_name("qe_ic"),
.name = "qe_ic",
};
static struct sys_device device_qe_ic = {

View file

@ -614,7 +614,7 @@ int ipic_get_irq(void)
}
static struct sysdev_class ipic_sysclass = {
set_kset_name("ipic"),
.name = "ipic",
};
static struct sys_device device_ipic = {

View file

@ -1043,7 +1043,7 @@ int openpic_resume(struct sys_device *sysdev)
#endif /* CONFIG_PM */
static struct sysdev_class openpic_sysclass = {
set_kset_name("openpic"),
.name = "openpic",
};
static struct sys_device device_openpic = {

View file

@ -666,7 +666,7 @@ int openpic2_resume(struct sys_device *sysdev)
/* HACK ALERT */
static struct sysdev_class openpic2_sysclass = {
set_kset_name("openpic2"),
.name = "openpic2",
};
static struct sys_device device_openpic2 = {

View file

@ -490,7 +490,7 @@ static struct super_operations hypfs_s_ops = {
.show_options = hypfs_show_options,
};
static decl_subsys(s390, NULL, NULL);
static struct kobject *s390_kobj;
static int __init hypfs_init(void)
{
@ -506,17 +506,18 @@ static int __init hypfs_init(void)
goto fail_diag;
}
}
kobj_set_kset_s(&s390_subsys, hypervisor_subsys);
rc = subsystem_register(&s390_subsys);
if (rc)
s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
if (!s390_kobj) {
rc = -ENOMEM;;
goto fail_sysfs;
}
rc = register_filesystem(&hypfs_type);
if (rc)
goto fail_filesystem;
return 0;
fail_filesystem:
subsystem_unregister(&s390_subsys);
kobject_put(s390_kobj);
fail_sysfs:
if (!MACHINE_IS_VM)
hypfs_diag_exit();
@ -530,7 +531,7 @@ static void __exit hypfs_exit(void)
if (!MACHINE_IS_VM)
hypfs_diag_exit();
unregister_filesystem(&hypfs_type);
subsystem_unregister(&s390_subsys);
kobject_put(s390_kobj);
}
module_init(hypfs_init)

View file

@ -162,22 +162,25 @@ EXPORT_SYMBOL_GPL(diag308);
/* SYSFS */
#define DEFINE_IPL_ATTR_RO(_prefix, _name, _format, _value) \
static ssize_t sys_##_prefix##_##_name##_show(struct kset *kset, \
static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj, \
struct kobj_attribute *attr, \
char *page) \
{ \
return sprintf(page, _format, _value); \
} \
static struct subsys_attribute sys_##_prefix##_##_name##_attr = \
static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
__ATTR(_name, S_IRUGO, sys_##_prefix##_##_name##_show, NULL);
#define DEFINE_IPL_ATTR_RW(_prefix, _name, _fmt_out, _fmt_in, _value) \
static ssize_t sys_##_prefix##_##_name##_show(struct kset *kset, \
static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj, \
struct kobj_attribute *attr, \
char *page) \
{ \
return sprintf(page, _fmt_out, \
(unsigned long long) _value); \
} \
static ssize_t sys_##_prefix##_##_name##_store(struct kset *kset, \
static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
struct kobj_attribute *attr, \
const char *buf, size_t len) \
{ \
unsigned long long value; \
@ -186,25 +189,27 @@ static ssize_t sys_##_prefix##_##_name##_store(struct kset *kset, \
_value = value; \
return len; \
} \
static struct subsys_attribute sys_##_prefix##_##_name##_attr = \
static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
__ATTR(_name,(S_IRUGO | S_IWUSR), \
sys_##_prefix##_##_name##_show, \
sys_##_prefix##_##_name##_store);
#define DEFINE_IPL_ATTR_STR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)\
static ssize_t sys_##_prefix##_##_name##_show(struct kset *kset, \
static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj, \
struct kobj_attribute *attr, \
char *page) \
{ \
return sprintf(page, _fmt_out, _value); \
} \
static ssize_t sys_##_prefix##_##_name##_store(struct kset *kset, \
static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
struct kobj_attribute *attr, \
const char *buf, size_t len) \
{ \
if (sscanf(buf, _fmt_in, _value) != 1) \
return -EINVAL; \
return len; \
} \
static struct subsys_attribute sys_##_prefix##_##_name##_attr = \
static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
__ATTR(_name,(S_IRUGO | S_IWUSR), \
sys_##_prefix##_##_name##_show, \
sys_##_prefix##_##_name##_store);
@ -270,14 +275,16 @@ void __init setup_ipl_info(void)
struct ipl_info ipl_info;
EXPORT_SYMBOL_GPL(ipl_info);
static ssize_t ipl_type_show(struct kset *kset, char *page)
static ssize_t ipl_type_show(struct kobject *kobj, struct kobj_attribute *attr,
char *page)
{
return sprintf(page, "%s\n", ipl_type_str(ipl_info.type));
}
static struct subsys_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type);
static struct kobj_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type);
static ssize_t sys_ipl_device_show(struct kset *kset, char *page)
static ssize_t sys_ipl_device_show(struct kobject *kobj,
struct kobj_attribute *attr, char *page)
{
struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START;
@ -292,7 +299,7 @@ static ssize_t sys_ipl_device_show(struct kset *kset, char *page)
}
}
static struct subsys_attribute sys_ipl_device_attr =
static struct kobj_attribute sys_ipl_device_attr =
__ATTR(device, S_IRUGO, sys_ipl_device_show, NULL);
static ssize_t ipl_parameter_read(struct kobject *kobj, struct bin_attribute *attr,
@ -367,7 +374,8 @@ static struct attribute_group ipl_fcp_attr_group = {
/* CCW ipl device attributes */
static ssize_t ipl_ccw_loadparm_show(struct kset *kset, char *page)
static ssize_t ipl_ccw_loadparm_show(struct kobject *kobj,
struct kobj_attribute *attr, char *page)
{
char loadparm[LOADPARM_LEN + 1] = {};
@ -379,7 +387,7 @@ static ssize_t ipl_ccw_loadparm_show(struct kset *kset, char *page)
return sprintf(page, "%s\n", loadparm);
}
static struct subsys_attribute sys_ipl_ccw_loadparm_attr =
static struct kobj_attribute sys_ipl_ccw_loadparm_attr =
__ATTR(loadparm, 0444, ipl_ccw_loadparm_show, NULL);
static struct attribute *ipl_ccw_attrs[] = {
@ -418,7 +426,7 @@ static struct attribute_group ipl_unknown_attr_group = {
.attrs = ipl_unknown_attrs,
};
static decl_subsys(ipl, NULL, NULL);
static struct kset *ipl_kset;
/*
* reipl section
@ -465,7 +473,8 @@ static void reipl_get_ascii_loadparm(char *loadparm)
strstrip(loadparm);
}
static ssize_t reipl_ccw_loadparm_show(struct kset *kset, char *page)
static ssize_t reipl_ccw_loadparm_show(struct kobject *kobj,
struct kobj_attribute *attr, char *page)
{
char buf[LOADPARM_LEN + 1];
@ -473,7 +482,8 @@ static ssize_t reipl_ccw_loadparm_show(struct kset *kset, char *page)
return sprintf(page, "%s\n", buf);
}
static ssize_t reipl_ccw_loadparm_store(struct kset *kset,
static ssize_t reipl_ccw_loadparm_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t len)
{
int i, lp_len;
@ -500,7 +510,7 @@ static ssize_t reipl_ccw_loadparm_store(struct kset *kset,
return len;
}
static struct subsys_attribute sys_reipl_ccw_loadparm_attr =
static struct kobj_attribute sys_reipl_ccw_loadparm_attr =
__ATTR(loadparm, 0644, reipl_ccw_loadparm_show,
reipl_ccw_loadparm_store);
@ -568,13 +578,15 @@ static int reipl_set_type(enum ipl_type type)
return 0;
}
static ssize_t reipl_type_show(struct kset *kset, char *page)
static ssize_t reipl_type_show(struct kobject *kobj,
struct kobj_attribute *attr, char *page)
{
return sprintf(page, "%s\n", ipl_type_str(reipl_type));
}
static ssize_t reipl_type_store(struct kset *kset, const char *buf,
size_t len)
static ssize_t reipl_type_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t len)
{
int rc = -EINVAL;
@ -587,10 +599,10 @@ static ssize_t reipl_type_store(struct kset *kset, const char *buf,
return (rc != 0) ? rc : len;
}
static struct subsys_attribute reipl_type_attr =
static struct kobj_attribute reipl_type_attr =
__ATTR(reipl_type, 0644, reipl_type_show, reipl_type_store);
static decl_subsys(reipl, NULL, NULL);
static struct kset *reipl_kset;
/*
* dump section
@ -663,13 +675,15 @@ static int dump_set_type(enum dump_type type)
return 0;
}
static ssize_t dump_type_show(struct kset *kset, char *page)
static ssize_t dump_type_show(struct kobject *kobj,
struct kobj_attribute *attr, char *page)
{
return sprintf(page, "%s\n", dump_type_str(dump_type));
}
static ssize_t dump_type_store(struct kset *kset, const char *buf,
size_t len)
static ssize_t dump_type_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t len)
{
int rc = -EINVAL;
@ -682,26 +696,28 @@ static ssize_t dump_type_store(struct kset *kset, const char *buf,
return (rc != 0) ? rc : len;
}
static struct subsys_attribute dump_type_attr =
static struct kobj_attribute dump_type_attr =
__ATTR(dump_type, 0644, dump_type_show, dump_type_store);
static decl_subsys(dump, NULL, NULL);
static struct kset *dump_kset;
/*
* Shutdown actions section
*/
static decl_subsys(shutdown_actions, NULL, NULL);
static struct kset *shutdown_actions_kset;
/* on panic */
static ssize_t on_panic_show(struct kset *kset, char *page)
static ssize_t on_panic_show(struct kobject *kobj,
struct kobj_attribute *attr, char *page)
{
return sprintf(page, "%s\n", shutdown_action_str(on_panic_action));
}
static ssize_t on_panic_store(struct kset *kset, const char *buf,
size_t len)
static ssize_t on_panic_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t len)
{
if (strncmp(buf, SHUTDOWN_REIPL_STR, strlen(SHUTDOWN_REIPL_STR)) == 0)
on_panic_action = SHUTDOWN_REIPL;
@ -717,7 +733,7 @@ static ssize_t on_panic_store(struct kset *kset, const char *buf,
return len;
}
static struct subsys_attribute on_panic_attr =
static struct kobj_attribute on_panic_attr =
__ATTR(on_panic, 0644, on_panic_show, on_panic_store);
void do_reipl(void)
@ -814,23 +830,23 @@ static int __init ipl_register_fcp_files(void)
{
int rc;
rc = sysfs_create_group(&ipl_subsys.kobj,
rc = sysfs_create_group(&ipl_kset->kobj,
&ipl_fcp_attr_group);
if (rc)
goto out;
rc = sysfs_create_bin_file(&ipl_subsys.kobj,
rc = sysfs_create_bin_file(&ipl_kset->kobj,
&ipl_parameter_attr);
if (rc)
goto out_ipl_parm;
rc = sysfs_create_bin_file(&ipl_subsys.kobj,
rc = sysfs_create_bin_file(&ipl_kset->kobj,
&ipl_scp_data_attr);
if (!rc)
goto out;
sysfs_remove_bin_file(&ipl_subsys.kobj, &ipl_parameter_attr);
sysfs_remove_bin_file(&ipl_kset->kobj, &ipl_parameter_attr);
out_ipl_parm:
sysfs_remove_group(&ipl_subsys.kobj, &ipl_fcp_attr_group);
sysfs_remove_group(&ipl_kset->kobj, &ipl_fcp_attr_group);
out:
return rc;
}
@ -839,12 +855,12 @@ static int __init ipl_init(void)
{
int rc;
rc = firmware_register(&ipl_subsys);
if (rc)
return rc;
ipl_kset = kset_create_and_add("ipl", NULL, firmware_kobj);
if (!ipl_kset)
return -ENOMEM;
switch (ipl_info.type) {
case IPL_TYPE_CCW:
rc = sysfs_create_group(&ipl_subsys.kobj,
rc = sysfs_create_group(&ipl_kset->kobj,
&ipl_ccw_attr_group);
break;
case IPL_TYPE_FCP:
@ -852,16 +868,16 @@ static int __init ipl_init(void)
rc = ipl_register_fcp_files();
break;
case IPL_TYPE_NSS:
rc = sysfs_create_group(&ipl_subsys.kobj,
rc = sysfs_create_group(&ipl_kset->kobj,
&ipl_nss_attr_group);
break;
default:
rc = sysfs_create_group(&ipl_subsys.kobj,
rc = sysfs_create_group(&ipl_kset->kobj,
&ipl_unknown_attr_group);
break;
}
if (rc)
firmware_unregister(&ipl_subsys);
kset_unregister(ipl_kset);
return rc;
}
@ -883,7 +899,7 @@ static int __init reipl_nss_init(void)
if (!MACHINE_IS_VM)
return 0;
rc = sysfs_create_group(&reipl_subsys.kobj, &reipl_nss_attr_group);
rc = sysfs_create_group(&reipl_kset->kobj, &reipl_nss_attr_group);
if (rc)
return rc;
strncpy(reipl_nss_name, kernel_nss_name, NSS_NAME_SIZE + 1);
@ -898,7 +914,7 @@ static int __init reipl_ccw_init(void)
reipl_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
if (!reipl_block_ccw)
return -ENOMEM;
rc = sysfs_create_group(&reipl_subsys.kobj, &reipl_ccw_attr_group);
rc = sysfs_create_group(&reipl_kset->kobj, &reipl_ccw_attr_group);
if (rc) {
free_page((unsigned long)reipl_block_ccw);
return rc;
@ -936,7 +952,7 @@ static int __init reipl_fcp_init(void)
reipl_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
if (!reipl_block_fcp)
return -ENOMEM;
rc = sysfs_create_group(&reipl_subsys.kobj, &reipl_fcp_attr_group);
rc = sysfs_create_group(&reipl_kset->kobj, &reipl_fcp_attr_group);
if (rc) {
free_page((unsigned long)reipl_block_fcp);
return rc;
@ -958,12 +974,12 @@ static int __init reipl_init(void)
{
int rc;
rc = firmware_register(&reipl_subsys);
if (rc)
return rc;
rc = subsys_create_file(&reipl_subsys, &reipl_type_attr);
reipl_kset = kset_create_and_add("reipl", NULL, firmware_kobj);
if (!reipl_kset)
return -ENOMEM;
rc = sysfs_create_file(&reipl_kset->kobj, &reipl_type_attr.attr);
if (rc) {
firmware_unregister(&reipl_subsys);
kset_unregister(reipl_kset);
return rc;
}
rc = reipl_ccw_init();
@ -988,7 +1004,7 @@ static int __init dump_ccw_init(void)
dump_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
if (!dump_block_ccw)
return -ENOMEM;
rc = sysfs_create_group(&dump_subsys.kobj, &dump_ccw_attr_group);
rc = sysfs_create_group(&dump_kset->kobj, &dump_ccw_attr_group);
if (rc) {
free_page((unsigned long)dump_block_ccw);
return rc;
@ -1012,7 +1028,7 @@ static int __init dump_fcp_init(void)
dump_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
if (!dump_block_fcp)
return -ENOMEM;
rc = sysfs_create_group(&dump_subsys.kobj, &dump_fcp_attr_group);
rc = sysfs_create_group(&dump_kset->kobj, &dump_fcp_attr_group);
if (rc) {
free_page((unsigned long)dump_block_fcp);
return rc;
@ -1047,12 +1063,12 @@ static int __init dump_init(void)
{
int rc;
rc = firmware_register(&dump_subsys);
if (rc)
return rc;
rc = subsys_create_file(&dump_subsys, &dump_type_attr);
dump_kset = kset_create_and_add("dump", NULL, firmware_kobj);
if (!dump_kset)
return -ENOMEM;
rc = sysfs_create_file(&dump_kset->kobj, &dump_type_attr);
if (rc) {
firmware_unregister(&dump_subsys);
kset_unregister(dump_kset);
return rc;
}
rc = dump_ccw_init();
@ -1069,12 +1085,13 @@ static int __init shutdown_actions_init(void)
{
int rc;
rc = firmware_register(&shutdown_actions_subsys);
if (rc)
return rc;
rc = subsys_create_file(&shutdown_actions_subsys, &on_panic_attr);
shutdown_actions_kset = kset_create_and_add("shutdown_actions", NULL,
firmware_kobj);
if (!shutdown_actions_kset)
return -ENOMEM;
rc = sysfs_create_file(&shutdown_actions_kset->kobj, &on_panic_attr);
if (rc) {
firmware_unregister(&shutdown_actions_subsys);
kset_unregister(shutdown_actions_kset);
return rc;
}
atomic_notifier_chain_register(&panic_notifier_list,

View file

@ -1145,7 +1145,7 @@ static void etr_work_fn(struct work_struct *work)
* Sysfs interface functions
*/
static struct sysdev_class etr_sysclass = {
set_kset_name("etr")
.name = "etr",
};
static struct sys_device etr_port0_dev = {

View file

@ -19,7 +19,7 @@
#include <asm/dma.h>
static struct sysdev_class dma_sysclass = {
set_kset_name("dma"),
.name = "dma",
};
EXPORT_SYMBOL(dma_sysclass);

View file

@ -341,17 +341,18 @@ static int __devinit sq_sysdev_add(struct sys_device *sysdev)
{
unsigned int cpu = sysdev->id;
struct kobject *kobj;
int error;
sq_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
if (unlikely(!sq_kobject[cpu]))
return -ENOMEM;
kobj = sq_kobject[cpu];
kobj->parent = &sysdev->kobj;
kobject_set_name(kobj, "%s", "sq");
kobj->ktype = &ktype_percpu_entry;
return kobject_register(kobj);
error = kobject_init_and_add(kobj, &ktype_percpu_entry, &sysdev->kobj,
"%s", "sq");
if (!error)
kobject_uevent(kobj, KOBJ_ADD);
return error;
}
static int __devexit sq_sysdev_remove(struct sys_device *sysdev)
@ -359,7 +360,7 @@ static int __devexit sq_sysdev_remove(struct sys_device *sysdev)
unsigned int cpu = sysdev->id;
struct kobject *kobj = sq_kobject[cpu];
kobject_unregister(kobj);
kobject_put(kobj);
return 0;
}

View file

@ -174,7 +174,7 @@ int timer_resume(struct sys_device *dev)
#endif
static struct sysdev_class timer_sysclass = {
set_kset_name("timer"),
.name = "timer",
.suspend = timer_suspend,
.resume = timer_resume,
};

View file

@ -1530,7 +1530,7 @@ static int lapic_resume(struct sys_device *dev)
*/
static struct sysdev_class lapic_sysclass = {
set_kset_name("lapic"),
.name = "lapic",
.resume = lapic_resume,
.suspend = lapic_suspend,
};

View file

@ -639,7 +639,7 @@ static int lapic_resume(struct sys_device *dev)
}
static struct sysdev_class lapic_sysclass = {
set_kset_name("lapic"),
.name = "lapic",
.resume = lapic_resume,
.suspend = lapic_suspend,
};

View file

@ -733,10 +733,8 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
if (unlikely(retval < 0))
return retval;
cache_kobject[cpu]->parent = &sys_dev->kobj;
kobject_set_name(cache_kobject[cpu], "%s", "cache");
cache_kobject[cpu]->ktype = &ktype_percpu_entry;
retval = kobject_register(cache_kobject[cpu]);
retval = kobject_init_and_add(cache_kobject[cpu], &ktype_percpu_entry,
&sys_dev->kobj, "%s", "cache");
if (retval < 0) {
cpuid4_cache_sysfs_exit(cpu);
return retval;
@ -746,23 +744,23 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
this_object = INDEX_KOBJECT_PTR(cpu,i);
this_object->cpu = cpu;
this_object->index = i;
this_object->kobj.parent = cache_kobject[cpu];
kobject_set_name(&(this_object->kobj), "index%1lu", i);
this_object->kobj.ktype = &ktype_cache;
retval = kobject_register(&(this_object->kobj));
retval = kobject_init_and_add(&(this_object->kobj),
&ktype_cache, cache_kobject[cpu],
"index%1lu", i);
if (unlikely(retval)) {
for (j = 0; j < i; j++) {
kobject_unregister(
&(INDEX_KOBJECT_PTR(cpu,j)->kobj));
kobject_put(&(INDEX_KOBJECT_PTR(cpu,j)->kobj));
}
kobject_unregister(cache_kobject[cpu]);
kobject_put(cache_kobject[cpu]);
cpuid4_cache_sysfs_exit(cpu);
break;
}
kobject_uevent(&(this_object->kobj), KOBJ_ADD);
}
if (!retval)
cpu_set(cpu, cache_dev_map);
kobject_uevent(cache_kobject[cpu], KOBJ_ADD);
return retval;
}
@ -778,8 +776,8 @@ static void __cpuinit cache_remove_dev(struct sys_device * sys_dev)
cpu_clear(cpu, cache_dev_map);
for (i = 0; i < num_cache_leaves; i++)
kobject_unregister(&(INDEX_KOBJECT_PTR(cpu,i)->kobj));
kobject_unregister(cache_kobject[cpu]);
kobject_put(&(INDEX_KOBJECT_PTR(cpu,i)->kobj));
kobject_put(cache_kobject[cpu]);
cpuid4_cache_sysfs_exit(cpu);
}

View file

@ -745,7 +745,7 @@ static void mce_restart(void)
static struct sysdev_class mce_sysclass = {
.resume = mce_resume,
set_kset_name("machinecheck"),
.name = "machinecheck",
};
DEFINE_PER_CPU(struct sys_device, device_mce);

View file

@ -65,7 +65,7 @@ static struct threshold_block threshold_defaults = {
};
struct threshold_bank {
struct kobject kobj;
struct kobject *kobj;
struct threshold_block *blocks;
cpumask_t cpus;
};
@ -432,10 +432,9 @@ static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
else
per_cpu(threshold_banks, cpu)[bank]->blocks = b;
kobject_set_name(&b->kobj, "misc%i", block);
b->kobj.parent = &per_cpu(threshold_banks, cpu)[bank]->kobj;
b->kobj.ktype = &threshold_ktype;
err = kobject_register(&b->kobj);
err = kobject_init_and_add(&b->kobj, &threshold_ktype,
per_cpu(threshold_banks, cpu)[bank]->kobj,
"misc%i", block);
if (err)
goto out_free;
recurse:
@ -451,11 +450,13 @@ static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
if (err)
goto out_free;
kobject_uevent(&b->kobj, KOBJ_ADD);
return err;
out_free:
if (b) {
kobject_unregister(&b->kobj);
kobject_put(&b->kobj);
kfree(b);
}
return err;
@ -489,7 +490,7 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
goto out;
err = sysfs_create_link(&per_cpu(device_mce, cpu).kobj,
&b->kobj, name);
b->kobj, name);
if (err)
goto out;
@ -505,16 +506,15 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
goto out;
}
kobject_set_name(&b->kobj, "threshold_bank%i", bank);
b->kobj.parent = &per_cpu(device_mce, cpu).kobj;
b->kobj = kobject_create_and_add(name, &per_cpu(device_mce, cpu).kobj);
if (!b->kobj)
goto out_free;
#ifndef CONFIG_SMP
b->cpus = CPU_MASK_ALL;
#else
b->cpus = per_cpu(cpu_core_map, cpu);
#endif
err = kobject_register(&b->kobj);
if (err)
goto out_free;
per_cpu(threshold_banks, cpu)[bank] = b;
@ -531,7 +531,7 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
continue;
err = sysfs_create_link(&per_cpu(device_mce, i).kobj,
&b->kobj, name);
b->kobj, name);
if (err)
goto out;
@ -581,7 +581,7 @@ static void deallocate_threshold_block(unsigned int cpu,
return;
list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
kobject_unregister(&pos->kobj);
kobject_put(&pos->kobj);
list_del(&pos->miscj);
kfree(pos);
}
@ -627,7 +627,7 @@ static void threshold_remove_bank(unsigned int cpu, int bank)
deallocate_threshold_block(cpu, bank);
free_out:
kobject_unregister(&b->kobj);
kobject_put(b->kobj);
kfree(b);
per_cpu(threshold_banks, cpu)[bank] = NULL;
}

View file

@ -157,15 +157,15 @@ static int __cpuinit cpuid_class_cpu_callback(struct notifier_block *nfb,
switch (action) {
case CPU_UP_PREPARE:
case CPU_UP_PREPARE_FROZEN:
err = cpuid_device_create(cpu);
break;
case CPU_UP_CANCELED:
case CPU_UP_CANCELED_FROZEN:
case CPU_DEAD:
case CPU_DEAD_FROZEN:
cpuid_device_destroy(cpu);
break;
case CPU_UP_CANCELED_FROZEN:
destroy_suspended_device(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
break;
}
return err ? NOTIFY_BAD : NOTIFY_OK;
}

View file

@ -51,7 +51,7 @@ static int i8237A_suspend(struct sys_device *dev, pm_message_t state)
}
static struct sysdev_class i8237_sysdev_class = {
set_kset_name("i8237"),
.name = "i8237",
.suspend = i8237A_suspend,
.resume = i8237A_resume,
};

View file

@ -258,7 +258,7 @@ static int i8259A_shutdown(struct sys_device *dev)
}
static struct sysdev_class i8259_sysdev_class = {
set_kset_name("i8259"),
.name = "i8259",
.suspend = i8259A_suspend,
.resume = i8259A_resume,
.shutdown = i8259A_shutdown,

View file

@ -370,7 +370,7 @@ static int i8259A_shutdown(struct sys_device *dev)
}
static struct sysdev_class i8259_sysdev_class = {
set_kset_name("i8259"),
.name = "i8259",
.suspend = i8259A_suspend,
.resume = i8259A_resume,
.shutdown = i8259A_shutdown,

View file

@ -2401,7 +2401,7 @@ static int ioapic_resume(struct sys_device *dev)
}
static struct sysdev_class ioapic_sysdev_class = {
set_kset_name("ioapic"),
.name = "ioapic",
.suspend = ioapic_suspend,
.resume = ioapic_resume,
};

View file

@ -1850,7 +1850,7 @@ static int ioapic_resume(struct sys_device *dev)
}
static struct sysdev_class ioapic_sysdev_class = {
set_kset_name("ioapic"),
.name = "ioapic",
.suspend = ioapic_suspend,
.resume = ioapic_resume,
};

View file

@ -155,15 +155,15 @@ static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
switch (action) {
case CPU_UP_PREPARE:
case CPU_UP_PREPARE_FROZEN:
err = msr_device_create(cpu);
break;
case CPU_UP_CANCELED:
case CPU_UP_CANCELED_FROZEN:
case CPU_DEAD:
case CPU_DEAD_FROZEN:
msr_device_destroy(cpu);
break;
case CPU_UP_CANCELED_FROZEN:
destroy_suspended_device(msr_class, MKDEV(MSR_MAJOR, cpu));
break;
}
return err ? NOTIFY_BAD : NOTIFY_OK;
}

View file

@ -176,7 +176,7 @@ static int lapic_nmi_resume(struct sys_device *dev)
static struct sysdev_class nmi_sysclass = {
set_kset_name("lapic_nmi"),
.name = "lapic_nmi",
.resume = lapic_nmi_resume,
.suspend = lapic_nmi_suspend,
};

View file

@ -211,7 +211,7 @@ static int lapic_nmi_resume(struct sys_device *dev)
}
static struct sysdev_class nmi_sysclass = {
set_kset_name("lapic_nmi"),
.name = "lapic_nmi",
.resume = lapic_nmi_resume,
.suspend = lapic_nmi_suspend,
};

View file

@ -51,7 +51,7 @@ static int nmi_resume(struct sys_device *dev)
static struct sysdev_class oprofile_sysclass = {
set_kset_name("oprofile"),
.name = "oprofile",
.resume = nmi_resume,
.suspend = nmi_suspend,
};

View file

@ -185,9 +185,7 @@ static elevator_t *elevator_alloc(struct request_queue *q,
eq->ops = &e->ops;
eq->elevator_type = e;
kobject_init(&eq->kobj);
kobject_set_name(&eq->kobj, "%s", "iosched");
eq->kobj.ktype = &elv_ktype;
kobject_init(&eq->kobj, &elv_ktype);
mutex_init(&eq->sysfs_lock);
eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
@ -931,9 +929,7 @@ int elv_register_queue(struct request_queue *q)
elevator_t *e = q->elevator;
int error;
e->kobj.parent = &q->kobj;
error = kobject_add(&e->kobj);
error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
if (!error) {
struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
if (attr) {

View file

@ -17,8 +17,10 @@
#include <linux/buffer_head.h>
#include <linux/mutex.h>
struct kset block_subsys;
static DEFINE_MUTEX(block_subsys_lock);
static DEFINE_MUTEX(block_class_lock);
#ifndef CONFIG_SYSFS_DEPRECATED
struct kobject *block_depr;
#endif
/*
* Can be deleted altogether. Later.
@ -37,19 +39,17 @@ static inline int major_to_index(int major)
}
#ifdef CONFIG_PROC_FS
void blkdev_show(struct seq_file *f, off_t offset)
{
struct blk_major_name *dp;
if (offset < BLKDEV_MAJOR_HASH_SIZE) {
mutex_lock(&block_subsys_lock);
mutex_lock(&block_class_lock);
for (dp = major_names[offset]; dp; dp = dp->next)
seq_printf(f, "%3d %s\n", dp->major, dp->name);
mutex_unlock(&block_subsys_lock);
mutex_unlock(&block_class_lock);
}
}
#endif /* CONFIG_PROC_FS */
int register_blkdev(unsigned int major, const char *name)
@ -57,7 +57,7 @@ int register_blkdev(unsigned int major, const char *name)
struct blk_major_name **n, *p;
int index, ret = 0;
mutex_lock(&block_subsys_lock);
mutex_lock(&block_class_lock);
/* temporary */
if (major == 0) {
@ -102,7 +102,7 @@ int register_blkdev(unsigned int major, const char *name)
kfree(p);
}
out:
mutex_unlock(&block_subsys_lock);
mutex_unlock(&block_class_lock);
return ret;
}
@ -114,7 +114,7 @@ void unregister_blkdev(unsigned int major, const char *name)
struct blk_major_name *p = NULL;
int index = major_to_index(major);
mutex_lock(&block_subsys_lock);
mutex_lock(&block_class_lock);
for (n = &major_names[index]; *n; n = &(*n)->next)
if ((*n)->major == major)
break;
@ -124,7 +124,7 @@ void unregister_blkdev(unsigned int major, const char *name)
p = *n;
*n = p->next;
}
mutex_unlock(&block_subsys_lock);
mutex_unlock(&block_class_lock);
kfree(p);
}
@ -137,29 +137,30 @@ static struct kobj_map *bdev_map;
* range must be nonzero
* The hash chain is sorted on range, so that subranges can override.
*/
void blk_register_region(dev_t dev, unsigned long range, struct module *module,
void blk_register_region(dev_t devt, unsigned long range, struct module *module,
struct kobject *(*probe)(dev_t, int *, void *),
int (*lock)(dev_t, void *), void *data)
{
kobj_map(bdev_map, dev, range, module, probe, lock, data);
kobj_map(bdev_map, devt, range, module, probe, lock, data);
}
EXPORT_SYMBOL(blk_register_region);
void blk_unregister_region(dev_t dev, unsigned long range)
void blk_unregister_region(dev_t devt, unsigned long range)
{
kobj_unmap(bdev_map, dev, range);
kobj_unmap(bdev_map, devt, range);
}
EXPORT_SYMBOL(blk_unregister_region);
static struct kobject *exact_match(dev_t dev, int *part, void *data)
static struct kobject *exact_match(dev_t devt, int *part, void *data)
{
struct gendisk *p = data;
return &p->kobj;
return &p->dev.kobj;
}
static int exact_lock(dev_t dev, void *data)
static int exact_lock(dev_t devt, void *data)
{
struct gendisk *p = data;
@ -194,8 +195,6 @@ void unlink_gendisk(struct gendisk *disk)
disk->minors);
}
#define to_disk(obj) container_of(obj,struct gendisk,kobj)
/**
* get_gendisk - get partitioning information for a given device
* @dev: device to get partitioning information for
@ -203,10 +202,12 @@ void unlink_gendisk(struct gendisk *disk)
* This function gets the structure containing partitioning
* information for the given device @dev.
*/
struct gendisk *get_gendisk(dev_t dev, int *part)
struct gendisk *get_gendisk(dev_t devt, int *part)
{
struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
return kobj ? to_disk(kobj) : NULL;
struct kobject *kobj = kobj_lookup(bdev_map, devt, part);
struct device *dev = kobj_to_dev(kobj);
return kobj ? dev_to_disk(dev) : NULL;
}
/*
@ -216,13 +217,17 @@ struct gendisk *get_gendisk(dev_t dev, int *part)
*/
void __init printk_all_partitions(void)
{
int n;
struct device *dev;
struct gendisk *sgp;
char buf[BDEVNAME_SIZE];
int n;
mutex_lock(&block_subsys_lock);
mutex_lock(&block_class_lock);
/* For each block device... */
list_for_each_entry(sgp, &block_subsys.list, kobj.entry) {
char buf[BDEVNAME_SIZE];
list_for_each_entry(dev, &block_class.devices, node) {
if (dev->type != &disk_type)
continue;
sgp = dev_to_disk(dev);
/*
* Don't show empty devices or things that have been surpressed
*/
@ -255,38 +260,46 @@ void __init printk_all_partitions(void)
sgp->major, n + 1 + sgp->first_minor,
(unsigned long long)sgp->part[n]->nr_sects >> 1,
disk_name(sgp, n + 1, buf));
} /* partition subloop */
} /* Block device loop */
}
}
mutex_unlock(&block_subsys_lock);
return;
mutex_unlock(&block_class_lock);
}
#ifdef CONFIG_PROC_FS
/* iterator */
static void *part_start(struct seq_file *part, loff_t *pos)
{
struct list_head *p;
loff_t l = *pos;
loff_t k = *pos;
struct device *dev;
mutex_lock(&block_subsys_lock);
list_for_each(p, &block_subsys.list)
if (!l--)
return list_entry(p, struct gendisk, kobj.entry);
mutex_lock(&block_class_lock);
list_for_each_entry(dev, &block_class.devices, node) {
if (dev->type != &disk_type)
continue;
if (!k--)
return dev_to_disk(dev);
}
return NULL;
}
static void *part_next(struct seq_file *part, void *v, loff_t *pos)
{
struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
struct gendisk *gp = v;
struct device *dev;
++*pos;
return p==&block_subsys.list ? NULL :
list_entry(p, struct gendisk, kobj.entry);
list_for_each_entry(dev, &gp->dev.node, node) {
if (&dev->node == &block_class.devices)
return NULL;
if (dev->type == &disk_type)
return dev_to_disk(dev);
}
return NULL;
}
static void part_stop(struct seq_file *part, void *v)
{
mutex_unlock(&block_subsys_lock);
mutex_unlock(&block_class_lock);
}
static int show_partition(struct seq_file *part, void *v)
@ -295,7 +308,7 @@ static int show_partition(struct seq_file *part, void *v)
int n;
char buf[BDEVNAME_SIZE];
if (&sgp->kobj.entry == block_subsys.list.next)
if (&sgp->dev.node == block_class.devices.next)
seq_puts(part, "major minor #blocks name\n\n");
/* Don't show non-partitionable removeable devices or empty devices */
@ -325,110 +338,81 @@ static int show_partition(struct seq_file *part, void *v)
}
struct seq_operations partitions_op = {
.start =part_start,
.next = part_next,
.stop = part_stop,
.show = show_partition
.start = part_start,
.next = part_next,
.stop = part_stop,
.show = show_partition
};
#endif
extern int blk_dev_init(void);
static struct kobject *base_probe(dev_t dev, int *part, void *data)
static struct kobject *base_probe(dev_t devt, int *part, void *data)
{
if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
/* Make old-style 2.4 aliases work */
request_module("block-major-%d", MAJOR(dev));
request_module("block-major-%d", MAJOR(devt));
return NULL;
}
static int __init genhd_device_init(void)
{
int err;
bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
class_register(&block_class);
bdev_map = kobj_map_init(base_probe, &block_class_lock);
blk_dev_init();
err = subsystem_register(&block_subsys);
if (err < 0)
printk(KERN_WARNING "%s: subsystem_register error: %d\n",
__FUNCTION__, err);
return err;
#ifndef CONFIG_SYSFS_DEPRECATED
/* create top-level block dir */
block_depr = kobject_create_and_add("block", NULL);
#endif
return 0;
}
subsys_initcall(genhd_device_init);
/*
* kobject & sysfs bindings for block devices
*/
static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
char *page)
static ssize_t disk_range_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gendisk *disk = to_disk(kobj);
struct disk_attribute *disk_attr =
container_of(attr,struct disk_attribute,attr);
ssize_t ret = -EIO;
struct gendisk *disk = dev_to_disk(dev);
if (disk_attr->show)
ret = disk_attr->show(disk,page);
return ret;
return sprintf(buf, "%d\n", disk->minors);
}
static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
const char *page, size_t count)
static ssize_t disk_removable_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gendisk *disk = to_disk(kobj);
struct disk_attribute *disk_attr =
container_of(attr,struct disk_attribute,attr);
ssize_t ret = 0;
struct gendisk *disk = dev_to_disk(dev);
if (disk_attr->store)
ret = disk_attr->store(disk, page, count);
return ret;
}
static struct sysfs_ops disk_sysfs_ops = {
.show = &disk_attr_show,
.store = &disk_attr_store,
};
static ssize_t disk_uevent_store(struct gendisk * disk,
const char *buf, size_t count)
{
kobject_uevent(&disk->kobj, KOBJ_ADD);
return count;
}
static ssize_t disk_dev_read(struct gendisk * disk, char *page)
{
dev_t base = MKDEV(disk->major, disk->first_minor);
return print_dev_t(page, base);
}
static ssize_t disk_range_read(struct gendisk * disk, char *page)
{
return sprintf(page, "%d\n", disk->minors);
}
static ssize_t disk_removable_read(struct gendisk * disk, char *page)
{
return sprintf(page, "%d\n",
return sprintf(buf, "%d\n",
(disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
}
}
static ssize_t disk_size_read(struct gendisk * disk, char *page)
static ssize_t disk_size_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
struct gendisk *disk = dev_to_disk(dev);
return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
}
static ssize_t disk_capability_read(struct gendisk *disk, char *page)
static ssize_t disk_capability_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(page, "%x\n", disk->flags);
struct gendisk *disk = dev_to_disk(dev);
return sprintf(buf, "%x\n", disk->flags);
}
static ssize_t disk_stats_read(struct gendisk * disk, char *page)
static ssize_t disk_stat_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gendisk *disk = dev_to_disk(dev);
preempt_disable();
disk_round_stats(disk);
preempt_enable();
return sprintf(page,
return sprintf(buf,
"%8lu %8lu %8llu %8u "
"%8lu %8lu %8llu %8u "
"%8u %8u %8u"
@ -445,40 +429,21 @@ static ssize_t disk_stats_read(struct gendisk * disk, char *page)
jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
}
static struct disk_attribute disk_attr_uevent = {
.attr = {.name = "uevent", .mode = S_IWUSR },
.store = disk_uevent_store
};
static struct disk_attribute disk_attr_dev = {
.attr = {.name = "dev", .mode = S_IRUGO },
.show = disk_dev_read
};
static struct disk_attribute disk_attr_range = {
.attr = {.name = "range", .mode = S_IRUGO },
.show = disk_range_read
};
static struct disk_attribute disk_attr_removable = {
.attr = {.name = "removable", .mode = S_IRUGO },
.show = disk_removable_read
};
static struct disk_attribute disk_attr_size = {
.attr = {.name = "size", .mode = S_IRUGO },
.show = disk_size_read
};
static struct disk_attribute disk_attr_capability = {
.attr = {.name = "capability", .mode = S_IRUGO },
.show = disk_capability_read
};
static struct disk_attribute disk_attr_stat = {
.attr = {.name = "stat", .mode = S_IRUGO },
.show = disk_stats_read
};
#ifdef CONFIG_FAIL_MAKE_REQUEST
static ssize_t disk_fail_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gendisk *disk = dev_to_disk(dev);
static ssize_t disk_fail_store(struct gendisk * disk,
return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
}
static ssize_t disk_fail_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct gendisk *disk = dev_to_disk(dev);
int i;
if (count > 0 && sscanf(buf, "%d", &i) > 0) {
@ -490,136 +455,100 @@ static ssize_t disk_fail_store(struct gendisk * disk,
return count;
}
static ssize_t disk_fail_read(struct gendisk * disk, char *page)
{
return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
}
static struct disk_attribute disk_attr_fail = {
.attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
.store = disk_fail_store,
.show = disk_fail_read
};
#endif
static struct attribute * default_attrs[] = {
&disk_attr_uevent.attr,
&disk_attr_dev.attr,
&disk_attr_range.attr,
&disk_attr_removable.attr,
&disk_attr_size.attr,
&disk_attr_stat.attr,
&disk_attr_capability.attr,
static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
#ifdef CONFIG_FAIL_MAKE_REQUEST
&disk_attr_fail.attr,
static struct device_attribute dev_attr_fail =
__ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
#endif
NULL,
static struct attribute *disk_attrs[] = {
&dev_attr_range.attr,
&dev_attr_removable.attr,
&dev_attr_size.attr,
&dev_attr_capability.attr,
&dev_attr_stat.attr,
#ifdef CONFIG_FAIL_MAKE_REQUEST
&dev_attr_fail.attr,
#endif
NULL
};
static void disk_release(struct kobject * kobj)
static struct attribute_group disk_attr_group = {
.attrs = disk_attrs,
};
static struct attribute_group *disk_attr_groups[] = {
&disk_attr_group,
NULL
};
static void disk_release(struct device *dev)
{
struct gendisk *disk = to_disk(kobj);
struct gendisk *disk = dev_to_disk(dev);
kfree(disk->random);
kfree(disk->part);
free_disk_stats(disk);
kfree(disk);
}
struct class block_class = {
.name = "block",
};
static struct kobj_type ktype_block = {
struct device_type disk_type = {
.name = "disk",
.groups = disk_attr_groups,
.release = disk_release,
.sysfs_ops = &disk_sysfs_ops,
.default_attrs = default_attrs,
};
extern struct kobj_type ktype_part;
static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
{
struct kobj_type *ktype = get_ktype(kobj);
return ((ktype == &ktype_block) || (ktype == &ktype_part));
}
static int block_uevent(struct kset *kset, struct kobject *kobj,
struct kobj_uevent_env *env)
{
struct kobj_type *ktype = get_ktype(kobj);
struct device *physdev;
struct gendisk *disk;
struct hd_struct *part;
if (ktype == &ktype_block) {
disk = container_of(kobj, struct gendisk, kobj);
add_uevent_var(env, "MINOR=%u", disk->first_minor);
} else if (ktype == &ktype_part) {
disk = container_of(kobj->parent, struct gendisk, kobj);
part = container_of(kobj, struct hd_struct, kobj);
add_uevent_var(env, "MINOR=%u",
disk->first_minor + part->partno);
} else
return 0;
add_uevent_var(env, "MAJOR=%u", disk->major);
/* add physical device, backing this device */
physdev = disk->driverfs_dev;
if (physdev) {
char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
add_uevent_var(env, "PHYSDEVPATH=%s", path);
kfree(path);
if (physdev->bus)
add_uevent_var(env, "PHYSDEVBUS=%s", physdev->bus->name);
if (physdev->driver)
add_uevent_var(env, physdev->driver->name);
}
return 0;
}
static struct kset_uevent_ops block_uevent_ops = {
.filter = block_uevent_filter,
.uevent = block_uevent,
};
decl_subsys(block, &ktype_block, &block_uevent_ops);
/*
* aggregate disk stat collector. Uses the same stats that the sysfs
* entries do, above, but makes them available through one seq_file.
* Watching a few disks may be efficient through sysfs, but watching
* all of them will be more efficient through this interface.
*
* The output looks suspiciously like /proc/partitions with a bunch of
* extra fields.
*/
/* iterator */
static void *diskstats_start(struct seq_file *part, loff_t *pos)
{
loff_t k = *pos;
struct list_head *p;
struct device *dev;
mutex_lock(&block_subsys_lock);
list_for_each(p, &block_subsys.list)
mutex_lock(&block_class_lock);
list_for_each_entry(dev, &block_class.devices, node) {
if (dev->type != &disk_type)
continue;
if (!k--)
return list_entry(p, struct gendisk, kobj.entry);
return dev_to_disk(dev);
}
return NULL;
}
static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
{
struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
struct gendisk *gp = v;
struct device *dev;
++*pos;
return p==&block_subsys.list ? NULL :
list_entry(p, struct gendisk, kobj.entry);
list_for_each_entry(dev, &gp->dev.node, node) {
if (&dev->node == &block_class.devices)
return NULL;
if (dev->type == &disk_type)
return dev_to_disk(dev);
}
return NULL;
}
static void diskstats_stop(struct seq_file *part, void *v)
{
mutex_unlock(&block_subsys_lock);
mutex_unlock(&block_class_lock);
}
static int diskstats_show(struct seq_file *s, void *v)
@ -629,7 +558,7 @@ static int diskstats_show(struct seq_file *s, void *v)
int n = 0;
/*
if (&sgp->kobj.entry == block_subsys.kset.list.next)
if (&gp->dev.kobj.entry == block_class.devices.next)
seq_puts(s, "major minor name"
" rio rmerge rsect ruse wio wmerge "
"wsect wuse running use aveq"
@ -683,7 +612,7 @@ static void media_change_notify_thread(struct work_struct *work)
* set enviroment vars to indicate which event this is for
* so that user space will know to go check the media status.
*/
kobject_uevent_env(&gd->kobj, KOBJ_CHANGE, envp);
kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
put_device(gd->driverfs_dev);
}
@ -694,6 +623,25 @@ void genhd_media_change_notify(struct gendisk *disk)
}
EXPORT_SYMBOL_GPL(genhd_media_change_notify);
dev_t blk_lookup_devt(const char *name)
{
struct device *dev;
dev_t devt = MKDEV(0, 0);
mutex_lock(&block_class_lock);
list_for_each_entry(dev, &block_class.devices, node) {
if (strcmp(dev->bus_id, name) == 0) {
devt = dev->devt;
break;
}
}
mutex_unlock(&block_class_lock);
return devt;
}
EXPORT_SYMBOL(blk_lookup_devt);
struct gendisk *alloc_disk(int minors)
{
return alloc_disk_node(minors, -1);
@ -721,9 +669,10 @@ struct gendisk *alloc_disk_node(int minors, int node_id)
}
}
disk->minors = minors;
kobj_set_kset_s(disk,block_subsys);
kobject_init(&disk->kobj);
rand_initialize_disk(disk);
disk->dev.class = &block_class;
disk->dev.type = &disk_type;
device_initialize(&disk->dev);
INIT_WORK(&disk->async_notify,
media_change_notify_thread);
}
@ -743,7 +692,7 @@ struct kobject *get_disk(struct gendisk *disk)
owner = disk->fops->owner;
if (owner && !try_module_get(owner))
return NULL;
kobj = kobject_get(&disk->kobj);
kobj = kobject_get(&disk->dev.kobj);
if (kobj == NULL) {
module_put(owner);
return NULL;
@ -757,7 +706,7 @@ EXPORT_SYMBOL(get_disk);
void put_disk(struct gendisk *disk)
{
if (disk)
kobject_put(&disk->kobj);
kobject_put(&disk->dev.kobj);
}
EXPORT_SYMBOL(put_disk);

View file

@ -1862,9 +1862,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
init_timer(&q->unplug_timer);
kobject_set_name(&q->kobj, "%s", "queue");
q->kobj.ktype = &queue_ktype;
kobject_init(&q->kobj);
kobject_init(&q->kobj, &queue_ktype);
mutex_init(&q->sysfs_lock);
@ -4182,9 +4180,8 @@ int blk_register_queue(struct gendisk *disk)
if (!q || !q->request_fn)
return -ENXIO;
q->kobj.parent = kobject_get(&disk->kobj);
ret = kobject_add(&q->kobj);
ret = kobject_add(&q->kobj, kobject_get(&disk->dev.kobj),
"%s", "queue");
if (ret < 0)
return ret;
@ -4209,6 +4206,6 @@ void blk_unregister_queue(struct gendisk *disk)
kobject_uevent(&q->kobj, KOBJ_REMOVE);
kobject_del(&q->kobj);
kobject_put(&disk->kobj);
kobject_put(&disk->dev.kobj);
}
}

View file

@ -743,7 +743,7 @@ static int __init acpi_bus_init(void)
return -ENODEV;
}
decl_subsys(acpi, NULL, NULL);
struct kobject *acpi_kobj;
static int __init acpi_init(void)
{
@ -755,10 +755,11 @@ static int __init acpi_init(void)
return -ENODEV;
}
result = firmware_register(&acpi_subsys);
if (result < 0)
printk(KERN_WARNING "%s: firmware_register error: %d\n",
__FUNCTION__, result);
acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
if (!acpi_kobj) {
printk(KERN_WARNING "%s: kset create error\n", __FUNCTION__);
acpi_kobj = NULL;
}
result = acpi_bus_init();

View file

@ -911,7 +911,7 @@ __setup("acpi_irq_balance", acpi_irq_balance_set);
/* FIXME: we will remove this interface after all drivers call pci_disable_device */
static struct sysdev_class irqrouter_sysdev_class = {
set_kset_name("irqrouter"),
.name = "irqrouter",
.resume = irqrouter_resume,
};

View file

@ -58,7 +58,7 @@ module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
FS Interface (/sys)
-------------------------------------------------------------------------- */
static LIST_HEAD(acpi_table_attr_list);
static struct kobject tables_kobj;
static struct kobject *tables_kobj;
struct acpi_table_attr {
struct bin_attribute attr;
@ -135,11 +135,9 @@ static int acpi_system_sysfs_init(void)
int table_index = 0;
int result;
tables_kobj.parent = &acpi_subsys.kobj;
kobject_set_name(&tables_kobj, "tables");
result = kobject_register(&tables_kobj);
if (result)
return result;
tables_kobj = kobject_create_and_add("tables", acpi_kobj);
if (!tables_kobj)
return -ENOMEM;
do {
result = acpi_get_table_by_index(table_index, &table_header);
@ -153,7 +151,7 @@ static int acpi_system_sysfs_init(void)
acpi_table_attr_init(table_attr, table_header);
result =
sysfs_create_bin_file(&tables_kobj,
sysfs_create_bin_file(tables_kobj,
&table_attr->attr);
if (result) {
kfree(table_attr);
@ -163,6 +161,7 @@ static int acpi_system_sysfs_init(void)
&acpi_table_attr_list);
}
} while (!result);
kobject_uevent(tables_kobj, KOBJ_ADD);
return 0;
}

View file

@ -11,6 +11,9 @@ obj-$(CONFIG_FW_LOADER) += firmware_class.o
obj-$(CONFIG_NUMA) += node.o
obj-$(CONFIG_MEMORY_HOTPLUG_SPARSE) += memory.o
obj-$(CONFIG_SMP) += topology.o
ifeq ($(CONFIG_SYSFS),y)
obj-$(CONFIG_MODULES) += module.o
endif
obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o
ifeq ($(CONFIG_DEBUG_DRIVER),y)

View file

@ -61,7 +61,7 @@ attribute_container_classdev_to_container(struct class_device *classdev)
}
EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
static struct list_head attribute_container_list;
static LIST_HEAD(attribute_container_list);
static DEFINE_MUTEX(attribute_container_mutex);
@ -429,10 +429,3 @@ attribute_container_find_class_device(struct attribute_container *cont,
return cdev;
}
EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
int __init
attribute_container_init(void)
{
INIT_LIST_HEAD(&attribute_container_list);
return 0;
}

View file

@ -1,6 +1,42 @@
/**
* struct bus_type_private - structure to hold the private to the driver core portions of the bus_type structure.
*
* @subsys - the struct kset that defines this bus. This is the main kobject
* @drivers_kset - the list of drivers associated with this bus
* @devices_kset - the list of devices associated with this bus
* @klist_devices - the klist to iterate over the @devices_kset
* @klist_drivers - the klist to iterate over the @drivers_kset
* @bus_notifier - the bus notifier list for anything that cares about things
* on this bus.
* @bus - pointer back to the struct bus_type that this structure is associated
* with.
*
* This structure is the one that is the actual kobject allowing struct
* bus_type to be statically allocated safely. Nothing outside of the driver
* core should ever touch these fields.
*/
struct bus_type_private {
struct kset subsys;
struct kset *drivers_kset;
struct kset *devices_kset;
struct klist klist_devices;
struct klist klist_drivers;
struct blocking_notifier_head bus_notifier;
unsigned int drivers_autoprobe:1;
struct bus_type *bus;
};
struct driver_private {
struct kobject kobj;
struct klist klist_devices;
struct klist_node knode_bus;
struct module_kobject *mkobj;
struct device_driver *driver;
};
#define to_driver(obj) container_of(obj, struct driver_private, kobj)
/* initialisation functions */
extern int devices_init(void);
extern int buses_init(void);
extern int classes_init(void);
@ -13,17 +49,16 @@ static inline int hypervisor_init(void) { return 0; }
extern int platform_bus_init(void);
extern int system_bus_init(void);
extern int cpu_dev_init(void);
extern int attribute_container_init(void);
extern int bus_add_device(struct device * dev);
extern void bus_attach_device(struct device * dev);
extern void bus_remove_device(struct device * dev);
extern int bus_add_device(struct device *dev);
extern void bus_attach_device(struct device *dev);
extern void bus_remove_device(struct device *dev);
extern int bus_add_driver(struct device_driver *);
extern void bus_remove_driver(struct device_driver *);
extern int bus_add_driver(struct device_driver *drv);
extern void bus_remove_driver(struct device_driver *drv);
extern void driver_detach(struct device_driver * drv);
extern int driver_probe_device(struct device_driver *, struct device *);
extern void driver_detach(struct device_driver *drv);
extern int driver_probe_device(struct device_driver *drv, struct device *dev);
extern void sysdev_shutdown(void);
extern int sysdev_suspend(pm_message_t state);
@ -44,4 +79,13 @@ extern char *make_class_name(const char *name, struct kobject *kobj);
extern int devres_release_all(struct device *dev);
extern struct kset devices_subsys;
extern struct kset *devices_kset;
#if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
extern void module_add_driver(struct module *mod, struct device_driver *drv);
extern void module_remove_driver(struct device_driver *drv);
#else
static inline void module_add_driver(struct module *mod,
struct device_driver *drv) { }
static inline void module_remove_driver(struct device_driver *drv) { }
#endif

View file

@ -3,6 +3,8 @@
*
* Copyright (c) 2002-3 Patrick Mochel
* Copyright (c) 2002-3 Open Source Development Labs
* Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
* Copyright (c) 2007 Novell Inc.
*
* This file is released under the GPLv2
*
@ -17,14 +19,13 @@
#include "power/power.h"
#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
#define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj)
#define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj)
/*
* sysfs bindings for drivers
*/
#define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
#define to_driver(obj) container_of(obj, struct device_driver, kobj)
static 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,
static struct bus_type *bus_get(struct bus_type *bus)
{
return bus ? container_of(kset_get(&bus->subsys),
struct bus_type, subsys) : NULL;
if (bus) {
kset_get(&bus->p->subsys);
return bus;
}
return NULL;
}
static void bus_put(struct bus_type *bus)
{
kset_put(&bus->subsys);
if (bus)
kset_put(&bus->p->subsys);
}
static ssize_t
drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct driver_attribute * drv_attr = to_drv_attr(attr);
struct device_driver * drv = to_driver(kobj);
struct driver_attribute *drv_attr = to_drv_attr(attr);
struct driver_private *drv_priv = to_driver(kobj);
ssize_t ret = -EIO;
if (drv_attr->show)
ret = drv_attr->show(drv, buf);
ret = drv_attr->show(drv_priv->driver, buf);
return ret;
}
static ssize_t
drv_attr_store(struct kobject * kobj, struct attribute * attr,
const char * buf, size_t count)
static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
struct driver_attribute * drv_attr = to_drv_attr(attr);
struct device_driver * drv = to_driver(kobj);
struct driver_attribute *drv_attr = to_drv_attr(attr);
struct driver_private *drv_priv = to_driver(kobj);
ssize_t ret = -EIO;
if (drv_attr->store)
ret = drv_attr->store(drv, buf, count);
ret = drv_attr->store(drv_priv->driver, buf, count);
return ret;
}
@ -71,22 +75,12 @@ static struct sysfs_ops driver_sysfs_ops = {
.store = drv_attr_store,
};
static void driver_release(struct kobject * kobj)
static void driver_release(struct kobject *kobj)
{
/*
* Yes this is an empty release function, it is this way because struct
* device is always a static object, not a dynamic one. Yes, this is
* not nice and bad, but remember, drivers are code, reference counted
* by the module count, not a device, which is really data. And yes,
* in the future I do want to have all drivers be created dynamically,
* and am working toward that goal, but it will take a bit longer...
*
* But do not let this example give _anyone_ the idea that they can
* create a release function without any code in it at all, to do that
* is almost always wrong. If you have any questions about this,
* please send an email to <greg@kroah.com>
*/
struct driver_private *drv_priv = to_driver(kobj);
pr_debug("driver: '%s': %s\n", kobject_name(kobj), __FUNCTION__);
kfree(drv_priv);
}
static struct kobj_type driver_ktype = {
@ -94,34 +88,30 @@ static struct kobj_type driver_ktype = {
.release = driver_release,
};
/*
* sysfs bindings for buses
*/
static ssize_t
bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct bus_attribute * bus_attr = to_bus_attr(attr);
struct bus_type * bus = to_bus(kobj);
struct bus_attribute *bus_attr = to_bus_attr(attr);
struct bus_type_private *bus_priv = to_bus(kobj);
ssize_t ret = 0;
if (bus_attr->show)
ret = bus_attr->show(bus, buf);
ret = bus_attr->show(bus_priv->bus, buf);
return ret;
}
static ssize_t
bus_attr_store(struct kobject * kobj, struct attribute * attr,
const char * buf, size_t count)
static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
struct bus_attribute * bus_attr = to_bus_attr(attr);
struct bus_type * bus = to_bus(kobj);
struct bus_attribute *bus_attr = to_bus_attr(attr);
struct bus_type_private *bus_priv = to_bus(kobj);
ssize_t ret = 0;
if (bus_attr->store)
ret = bus_attr->store(bus, buf, count);
ret = bus_attr->store(bus_priv->bus, buf, count);
return ret;
}
@ -130,24 +120,26 @@ static struct sysfs_ops bus_sysfs_ops = {
.store = bus_attr_store,
};
int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
{
int error;
if (bus_get(bus)) {
error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
bus_put(bus);
} else
error = -EINVAL;
return error;
}
EXPORT_SYMBOL_GPL(bus_create_file);
void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
{
if (bus_get(bus)) {
sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
bus_put(bus);
}
}
EXPORT_SYMBOL_GPL(bus_remove_file);
static struct kobj_type bus_ktype = {
.sysfs_ops = &bus_sysfs_ops,
@ -166,7 +158,7 @@ static struct kset_uevent_ops bus_uevent_ops = {
.filter = bus_uevent_filter,
};
static decl_subsys(bus, &bus_ktype, &bus_uevent_ops);
static struct kset *bus_kset;
#ifdef CONFIG_HOTPLUG
@ -224,10 +216,13 @@ static ssize_t driver_bind(struct device_driver *drv,
if (dev->parent)
up(&dev->parent->sem);
if (err > 0) /* success */
if (err > 0) {
/* success */
err = count;
else if (err == 0) /* driver didn't accept device */
} else if (err == 0) {
/* driver didn't accept device */
err = -ENODEV;
}
}
put_device(dev);
bus_put(bus);
@ -237,16 +232,16 @@ static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
{
return sprintf(buf, "%d\n", bus->drivers_autoprobe);
return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
}
static ssize_t store_drivers_autoprobe(struct bus_type *bus,
const char *buf, size_t count)
{
if (buf[0] == '0')
bus->drivers_autoprobe = 0;
bus->p->drivers_autoprobe = 0;
else
bus->drivers_autoprobe = 1;
bus->p->drivers_autoprobe = 1;
return count;
}
@ -264,49 +259,49 @@ static ssize_t store_drivers_probe(struct bus_type *bus,
}
#endif
static struct device * next_device(struct klist_iter * i)
static struct device *next_device(struct klist_iter *i)
{
struct klist_node * n = klist_next(i);
struct klist_node *n = klist_next(i);
return n ? container_of(n, struct device, knode_bus) : NULL;
}
/**
* bus_for_each_dev - device iterator.
* @bus: bus type.
* @start: device to start iterating from.
* @data: data for the callback.
* @fn: function to be called for each device.
* bus_for_each_dev - device iterator.
* @bus: bus type.
* @start: device to start iterating from.
* @data: data for the callback.
* @fn: function to be called for each device.
*
* Iterate over @bus's list of devices, and call @fn for each,
* passing it @data. If @start is not NULL, we use that device to
* begin iterating from.
* Iterate over @bus's list of devices, and call @fn for each,
* passing it @data. If @start is not NULL, we use that device to
* begin iterating from.
*
* We check the return of @fn each time. If it returns anything
* other than 0, we break out and return that value.
* We check the return of @fn each time. If it returns anything
* other than 0, we break out and return that value.
*
* NOTE: The device that returns a non-zero value is not retained
* in any way, nor is its refcount incremented. If the caller needs
* to retain this data, it should do, and increment the reference
* count in the supplied callback.
* NOTE: The device that returns a non-zero value is not retained
* in any way, nor is its refcount incremented. If the caller needs
* to retain this data, it should do, and increment the reference
* count in the supplied callback.
*/
int bus_for_each_dev(struct bus_type * bus, struct device * start,
void * data, int (*fn)(struct device *, void *))
int bus_for_each_dev(struct bus_type *bus, struct device *start,
void *data, int (*fn)(struct device *, void *))
{
struct klist_iter i;
struct device * dev;
struct device *dev;
int error = 0;
if (!bus)
return -EINVAL;
klist_iter_init_node(&bus->klist_devices, &i,
klist_iter_init_node(&bus->p->klist_devices, &i,
(start ? &start->knode_bus : NULL));
while ((dev = next_device(&i)) && !error)
error = fn(dev, data);
klist_iter_exit(&i);
return error;
}
EXPORT_SYMBOL_GPL(bus_for_each_dev);
/**
* 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,
* if it does. If the callback returns non-zero, this function will
* return to the caller and not iterate over any more devices.
*/
struct device * bus_find_device(struct bus_type *bus,
struct device *start, void *data,
int (*match)(struct device *, void *))
struct device *bus_find_device(struct bus_type *bus,
struct device *start, void *data,
int (*match)(struct device *dev, void *data))
{
struct klist_iter i;
struct device *dev;
@ -333,7 +328,7 @@ struct device * bus_find_device(struct bus_type *bus,
if (!bus)
return NULL;
klist_iter_init_node(&bus->klist_devices, &i,
klist_iter_init_node(&bus->p->klist_devices, &i,
(start ? &start->knode_bus : NULL));
while ((dev = next_device(&i)))
if (match(dev, data) && get_device(dev))
@ -341,51 +336,57 @@ struct device * bus_find_device(struct bus_type *bus,
klist_iter_exit(&i);
return dev;
}
EXPORT_SYMBOL_GPL(bus_find_device);
static struct device_driver * next_driver(struct klist_iter * i)
static struct device_driver *next_driver(struct klist_iter *i)
{
struct klist_node * n = klist_next(i);
return n ? container_of(n, struct device_driver, knode_bus) : NULL;
struct klist_node *n = klist_next(i);
struct driver_private *drv_priv;
if (n) {
drv_priv = container_of(n, struct driver_private, knode_bus);
return drv_priv->driver;
}
return NULL;
}
/**
* bus_for_each_drv - driver iterator
* @bus: bus we're dealing with.
* @start: driver to start iterating on.
* @data: data to pass to the callback.
* @fn: function to call for each driver.
* bus_for_each_drv - driver iterator
* @bus: bus we're dealing with.
* @start: driver to start iterating on.
* @data: data to pass to the callback.
* @fn: function to call for each driver.
*
* This is nearly identical to the device iterator above.
* We iterate over each driver that belongs to @bus, and call
* @fn for each. If @fn returns anything but 0, we break out
* and return it. If @start is not NULL, we use it as the head
* of the list.
* This is nearly identical to the device iterator above.
* We iterate over each driver that belongs to @bus, and call
* @fn for each. If @fn returns anything but 0, we break out
* and return it. If @start is not NULL, we use it as the head
* of the list.
*
* NOTE: we don't return the driver that returns a non-zero
* value, nor do we leave the reference count incremented for that
* driver. If the caller needs to know that info, it must set it
* in the callback. It must also be sure to increment the refcount
* so it doesn't disappear before returning to the caller.
* NOTE: we don't return the driver that returns a non-zero
* value, nor do we leave the reference count incremented for that
* driver. If the caller needs to know that info, it must set it
* in the callback. It must also be sure to increment the refcount
* so it doesn't disappear before returning to the caller.
*/
int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
void * data, int (*fn)(struct device_driver *, void *))
int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
void *data, int (*fn)(struct device_driver *, void *))
{
struct klist_iter i;
struct device_driver * drv;
struct device_driver *drv;
int error = 0;
if (!bus)
return -EINVAL;
klist_iter_init_node(&bus->klist_drivers, &i,
start ? &start->knode_bus : NULL);
klist_iter_init_node(&bus->p->klist_drivers, &i,
start ? &start->p->knode_bus : NULL);
while ((drv = next_driver(&i)) && !error)
error = fn(drv, data);
klist_iter_exit(&i);
return error;
}
EXPORT_SYMBOL_GPL(bus_for_each_drv);
static int device_add_attrs(struct bus_type *bus, struct device *dev)
{
@ -396,7 +397,7 @@ static int device_add_attrs(struct bus_type *bus, struct device *dev)
return 0;
for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
error = device_create_file(dev,&bus->dev_attrs[i]);
error = device_create_file(dev, &bus->dev_attrs[i]);
if (error) {
while (--i >= 0)
device_remove_file(dev, &bus->dev_attrs[i]);
@ -406,13 +407,13 @@ static int device_add_attrs(struct bus_type *bus, struct device *dev)
return error;
}
static void device_remove_attrs(struct bus_type * bus, struct device * dev)
static void device_remove_attrs(struct bus_type *bus, struct device *dev)
{
int i;
if (bus->dev_attrs) {
for (i = 0; attr_name(bus->dev_attrs[i]); i++)
device_remove_file(dev,&bus->dev_attrs[i]);
device_remove_file(dev, &bus->dev_attrs[i]);
}
}
@ -420,7 +421,7 @@ static void device_remove_attrs(struct bus_type * bus, struct device * dev)
static int make_deprecated_bus_links(struct device *dev)
{
return sysfs_create_link(&dev->kobj,
&dev->bus->subsys.kobj, "bus");
&dev->bus->p->subsys.kobj, "bus");
}
static void remove_deprecated_bus_links(struct device *dev)
@ -433,28 +434,28 @@ static inline void remove_deprecated_bus_links(struct device *dev) { }
#endif
/**
* bus_add_device - add device to bus
* @dev: device being added
* bus_add_device - add device to bus
* @dev: device being added
*
* - Add the device to its bus's list of devices.
* - Create link to device's bus.
* - Add the device to its bus's list of devices.
* - Create link to device's bus.
*/
int bus_add_device(struct device * dev)
int bus_add_device(struct device *dev)
{
struct bus_type * bus = bus_get(dev->bus);
struct bus_type *bus = bus_get(dev->bus);
int error = 0;
if (bus) {
pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
pr_debug("bus: '%s': add device %s\n", bus->name, dev->bus_id);
error = device_add_attrs(bus, dev);
if (error)
goto out_put;
error = sysfs_create_link(&bus->devices.kobj,
error = sysfs_create_link(&bus->p->devices_kset->kobj,
&dev->kobj, dev->bus_id);
if (error)
goto out_id;
error = sysfs_create_link(&dev->kobj,
&dev->bus->subsys.kobj, "subsystem");
&dev->bus->p->subsys.kobj, "subsystem");
if (error)
goto out_subsys;
error = make_deprecated_bus_links(dev);
@ -466,7 +467,7 @@ int bus_add_device(struct device * dev)
out_deprecated:
sysfs_remove_link(&dev->kobj, "subsystem");
out_subsys:
sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
sysfs_remove_link(&bus->p->devices_kset->kobj, dev->bus_id);
out_id:
device_remove_attrs(bus, dev);
out_put:
@ -475,56 +476,58 @@ int bus_add_device(struct device * dev)
}
/**
* bus_attach_device - add device to bus
* @dev: device tried to attach to a driver
* bus_attach_device - add device to bus
* @dev: device tried to attach to a driver
*
* - Add device to bus's list of devices.
* - Try to attach to driver.
* - Add device to bus's list of devices.
* - Try to attach to driver.
*/
void bus_attach_device(struct device * dev)
void bus_attach_device(struct device *dev)
{
struct bus_type *bus = dev->bus;
int ret = 0;
if (bus) {
dev->is_registered = 1;
if (bus->drivers_autoprobe)
if (bus->p->drivers_autoprobe)
ret = device_attach(dev);
WARN_ON(ret < 0);
if (ret >= 0)
klist_add_tail(&dev->knode_bus, &bus->klist_devices);
klist_add_tail(&dev->knode_bus, &bus->p->klist_devices);
else
dev->is_registered = 0;
}
}
/**
* bus_remove_device - remove device from bus
* @dev: device to be removed
* bus_remove_device - remove device from bus
* @dev: device to be removed
*
* - Remove symlink from bus's directory.
* - Delete device from bus's list.
* - Detach from its driver.
* - Drop reference taken in bus_add_device().
* - Remove symlink from bus's directory.
* - Delete device from bus's list.
* - Detach from its driver.
* - Drop reference taken in bus_add_device().
*/
void bus_remove_device(struct device * dev)
void bus_remove_device(struct device *dev)
{
if (dev->bus) {
sysfs_remove_link(&dev->kobj, "subsystem");
remove_deprecated_bus_links(dev);
sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
dev->bus_id);
device_remove_attrs(dev->bus, dev);
if (dev->is_registered) {
dev->is_registered = 0;
klist_del(&dev->knode_bus);
}
pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
pr_debug("bus: '%s': remove device %s\n",
dev->bus->name, dev->bus_id);
device_release_driver(dev);
bus_put(dev->bus);
}
}
static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
{
int error = 0;
int i;
@ -533,19 +536,19 @@ static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
error = driver_create_file(drv, &bus->drv_attrs[i]);
if (error)
goto Err;
goto err;
}
}
Done:
done:
return error;
Err:
err:
while (--i >= 0)
driver_remove_file(drv, &bus->drv_attrs[i]);
goto Done;
goto done;
}
static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
static void driver_remove_attrs(struct bus_type *bus,
struct device_driver *drv)
{
int i;
@ -616,39 +619,46 @@ static ssize_t driver_uevent_store(struct device_driver *drv,
enum kobject_action action;
if (kobject_action_type(buf, count, &action) == 0)
kobject_uevent(&drv->kobj, action);
kobject_uevent(&drv->p->kobj, action);
return count;
}
static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
/**
* bus_add_driver - Add a driver to the bus.
* @drv: driver.
*
* bus_add_driver - Add a driver to the bus.
* @drv: driver.
*/
int bus_add_driver(struct device_driver *drv)
{
struct bus_type * bus = bus_get(drv->bus);
struct bus_type *bus;
struct driver_private *priv;
int error = 0;
bus = bus_get(drv->bus);
if (!bus)
return -EINVAL;
pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
error = kobject_set_name(&drv->kobj, "%s", drv->name);
if (error)
goto out_put_bus;
drv->kobj.kset = &bus->drivers;
error = kobject_register(&drv->kobj);
pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
klist_init(&priv->klist_devices, NULL, NULL);
priv->driver = drv;
drv->p = priv;
priv->kobj.kset = bus->p->drivers_kset;
error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
"%s", drv->name);
if (error)
goto out_put_bus;
if (drv->bus->drivers_autoprobe) {
if (drv->bus->p->drivers_autoprobe) {
error = driver_attach(drv);
if (error)
goto out_unregister;
}
klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
module_add_driver(drv->owner, drv);
error = driver_create_file(drv, &driver_attr_uevent);
@ -669,24 +679,24 @@ int bus_add_driver(struct device_driver *drv)
__FUNCTION__, drv->name);
}
kobject_uevent(&priv->kobj, KOBJ_ADD);
return error;
out_unregister:
kobject_unregister(&drv->kobj);
kobject_put(&priv->kobj);
out_put_bus:
bus_put(bus);
return error;
}
/**
* bus_remove_driver - delete driver from bus's knowledge.
* @drv: driver.
* bus_remove_driver - delete driver from bus's knowledge.
* @drv: driver.
*
* Detach the driver from the devices it controls, and remove
* it from its bus's list of drivers. Finally, we drop the reference
* to the bus we took in bus_add_driver().
* Detach the driver from the devices it controls, and remove
* it from its bus's list of drivers. Finally, we drop the reference
* to the bus we took in bus_add_driver().
*/
void bus_remove_driver(struct device_driver * drv)
void bus_remove_driver(struct device_driver *drv)
{
if (!drv->bus)
return;
@ -694,18 +704,17 @@ void bus_remove_driver(struct device_driver * drv)
remove_bind_files(drv);
driver_remove_attrs(drv->bus, drv);
driver_remove_file(drv, &driver_attr_uevent);
klist_remove(&drv->knode_bus);
pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
klist_remove(&drv->p->knode_bus);
pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
driver_detach(drv);
module_remove_driver(drv);
kobject_unregister(&drv->kobj);
kobject_put(&drv->p->kobj);
bus_put(drv->bus);
}
/* Helper for bus_rescan_devices's iter */
static int __must_check bus_rescan_devices_helper(struct device *dev,
void *data)
void *data)
{
int ret = 0;
@ -727,10 +736,11 @@ static int __must_check bus_rescan_devices_helper(struct device *dev,
* attached and rescan it against existing drivers to see if it matches
* any by calling device_attach() for the unbound devices.
*/
int bus_rescan_devices(struct bus_type * bus)
int bus_rescan_devices(struct bus_type *bus)
{
return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
}
EXPORT_SYMBOL_GPL(bus_rescan_devices);
/**
* device_reprobe - remove driver for a device and probe for a new driver
@ -755,55 +765,55 @@ int device_reprobe(struct device *dev)
EXPORT_SYMBOL_GPL(device_reprobe);
/**
* find_bus - locate bus by name.
* @name: name of bus.
* find_bus - locate bus by name.
* @name: name of bus.
*
* Call kset_find_obj() to iterate over list of buses to
* find a bus by name. Return bus if found.
* Call kset_find_obj() to iterate over list of buses to
* find a bus by name. Return bus if found.
*
* Note that kset_find_obj increments bus' reference count.
* Note that kset_find_obj increments bus' reference count.
*/
#if 0
struct bus_type * find_bus(char * name)
struct bus_type *find_bus(char *name)
{
struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
struct kobject *k = kset_find_obj(bus_kset, name);
return k ? to_bus(k) : NULL;
}
#endif /* 0 */
/**
* bus_add_attrs - Add default attributes for this bus.
* @bus: Bus that has just been registered.
* bus_add_attrs - Add default attributes for this bus.
* @bus: Bus that has just been registered.
*/
static int bus_add_attrs(struct bus_type * bus)
static int bus_add_attrs(struct bus_type *bus)
{
int error = 0;
int i;
if (bus->bus_attrs) {
for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
error = bus_create_file(bus,&bus->bus_attrs[i]);
error = bus_create_file(bus, &bus->bus_attrs[i]);
if (error)
goto Err;
goto err;
}
}
Done:
done:
return error;
Err:
err:
while (--i >= 0)
bus_remove_file(bus,&bus->bus_attrs[i]);
goto Done;
bus_remove_file(bus, &bus->bus_attrs[i]);
goto done;
}
static void bus_remove_attrs(struct bus_type * bus)
static void bus_remove_attrs(struct bus_type *bus)
{
int i;
if (bus->bus_attrs) {
for (i = 0; attr_name(bus->bus_attrs[i]); i++)
bus_remove_file(bus,&bus->bus_attrs[i]);
bus_remove_file(bus, &bus->bus_attrs[i]);
}
}
@ -827,32 +837,42 @@ static ssize_t bus_uevent_store(struct bus_type *bus,
enum kobject_action action;
if (kobject_action_type(buf, count, &action) == 0)
kobject_uevent(&bus->subsys.kobj, action);
kobject_uevent(&bus->p->subsys.kobj, action);
return count;
}
static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
/**
* bus_register - register a bus with the system.
* @bus: bus.
* bus_register - register a bus with the system.
* @bus: bus.
*
* Once we have that, we registered the bus with the kobject
* infrastructure, then register the children subsystems it has:
* the devices and drivers that belong to the bus.
* Once we have that, we registered the bus with the kobject
* infrastructure, then register the children subsystems it has:
* the devices and drivers that belong to the bus.
*/
int bus_register(struct bus_type * bus)
int bus_register(struct bus_type *bus)
{
int retval;
struct bus_type_private *priv;
BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
if (!priv)
return -ENOMEM;
retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
priv->bus = bus;
bus->p = priv;
BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
if (retval)
goto out;
bus->subsys.kobj.kset = &bus_subsys;
priv->subsys.kobj.kset = bus_kset;
priv->subsys.kobj.ktype = &bus_ktype;
priv->drivers_autoprobe = 1;
retval = subsystem_register(&bus->subsys);
retval = kset_register(&priv->subsys);
if (retval)
goto out;
@ -860,23 +880,23 @@ int bus_register(struct bus_type * bus)
if (retval)
goto bus_uevent_fail;
kobject_set_name(&bus->devices.kobj, "devices");
bus->devices.kobj.parent = &bus->subsys.kobj;
retval = kset_register(&bus->devices);
if (retval)
priv->devices_kset = kset_create_and_add("devices", NULL,
&priv->subsys.kobj);
if (!priv->devices_kset) {
retval = -ENOMEM;
goto bus_devices_fail;
}
kobject_set_name(&bus->drivers.kobj, "drivers");
bus->drivers.kobj.parent = &bus->subsys.kobj;
bus->drivers.ktype = &driver_ktype;
retval = kset_register(&bus->drivers);
if (retval)
priv->drivers_kset = kset_create_and_add("drivers", NULL,
&priv->subsys.kobj);
if (!priv->drivers_kset) {
retval = -ENOMEM;
goto bus_drivers_fail;
}
klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
klist_init(&bus->klist_drivers, NULL, NULL);
klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
klist_init(&priv->klist_drivers, NULL, NULL);
bus->drivers_autoprobe = 1;
retval = add_probe_files(bus);
if (retval)
goto bus_probe_files_fail;
@ -885,66 +905,73 @@ int bus_register(struct bus_type * bus)
if (retval)
goto bus_attrs_fail;
pr_debug("bus type '%s' registered\n", bus->name);
pr_debug("bus: '%s': registered\n", bus->name);
return 0;
bus_attrs_fail:
remove_probe_files(bus);
bus_probe_files_fail:
kset_unregister(&bus->drivers);
kset_unregister(bus->p->drivers_kset);
bus_drivers_fail:
kset_unregister(&bus->devices);
kset_unregister(bus->p->devices_kset);
bus_devices_fail:
bus_remove_file(bus, &bus_attr_uevent);
bus_uevent_fail:
subsystem_unregister(&bus->subsys);
kset_unregister(&bus->p->subsys);
kfree(bus->p);
out:
return retval;
}
EXPORT_SYMBOL_GPL(bus_register);
/**
* bus_unregister - remove a bus from the system
* @bus: bus.
* bus_unregister - remove a bus from the system
* @bus: bus.
*
* Unregister the child subsystems and the bus itself.
* Finally, we call bus_put() to release the refcount
* Unregister the child subsystems and the bus itself.
* Finally, we call bus_put() to release the refcount
*/
void bus_unregister(struct bus_type * bus)
void bus_unregister(struct bus_type *bus)
{
pr_debug("bus %s: unregistering\n", bus->name);
pr_debug("bus: '%s': unregistering\n", bus->name);
bus_remove_attrs(bus);
remove_probe_files(bus);
kset_unregister(&bus->drivers);
kset_unregister(&bus->devices);
kset_unregister(bus->p->drivers_kset);
kset_unregister(bus->p->devices_kset);
bus_remove_file(bus, &bus_attr_uevent);
subsystem_unregister(&bus->subsys);
kset_unregister(&bus->p->subsys);
kfree(bus->p);
}
EXPORT_SYMBOL_GPL(bus_unregister);
int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
{
return blocking_notifier_chain_register(&bus->bus_notifier, nb);
return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
}
EXPORT_SYMBOL_GPL(bus_register_notifier);
int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
{
return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
}
EXPORT_SYMBOL_GPL(bus_unregister_notifier);
struct kset *bus_get_kset(struct bus_type *bus)
{
return &bus->p->subsys;
}
EXPORT_SYMBOL_GPL(bus_get_kset);
struct klist *bus_get_device_klist(struct bus_type *bus)
{
return &bus->p->klist_devices;
}
EXPORT_SYMBOL_GPL(bus_get_device_klist);
int __init buses_init(void)
{
return subsystem_register(&bus_subsys);
bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
if (!bus_kset)
return -ENOMEM;
return 0;
}
EXPORT_SYMBOL_GPL(bus_for_each_dev);
EXPORT_SYMBOL_GPL(bus_find_device);
EXPORT_SYMBOL_GPL(bus_for_each_drv);
EXPORT_SYMBOL_GPL(bus_register);
EXPORT_SYMBOL_GPL(bus_unregister);
EXPORT_SYMBOL_GPL(bus_rescan_devices);
EXPORT_SYMBOL_GPL(bus_create_file);
EXPORT_SYMBOL_GPL(bus_remove_file);

View file

@ -17,16 +17,17 @@
#include <linux/kdev_t.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/genhd.h>
#include "base.h"
#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
#define to_class(obj) container_of(obj, struct class, subsys.kobj)
static ssize_t
class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct class_attribute * class_attr = to_class_attr(attr);
struct class * dc = to_class(kobj);
struct class_attribute *class_attr = to_class_attr(attr);
struct class *dc = to_class(kobj);
ssize_t ret = -EIO;
if (class_attr->show)
@ -34,12 +35,11 @@ class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
return ret;
}
static ssize_t
class_attr_store(struct kobject * kobj, struct attribute * attr,
const char * buf, size_t count)
static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
struct class_attribute * class_attr = to_class_attr(attr);
struct class * dc = to_class(kobj);
struct class_attribute *class_attr = to_class_attr(attr);
struct class *dc = to_class(kobj);
ssize_t ret = -EIO;
if (class_attr->store)
@ -47,7 +47,7 @@ class_attr_store(struct kobject * kobj, struct attribute * attr,
return ret;
}
static void class_release(struct kobject * kobj)
static void class_release(struct kobject *kobj)
{
struct class *class = to_class(kobj);
@ -71,20 +71,20 @@ static struct kobj_type class_ktype = {
};
/* Hotplug events for classes go to the class_obj subsys */
static decl_subsys(class, &class_ktype, NULL);
static struct kset *class_kset;
int class_create_file(struct class * cls, const struct class_attribute * attr)
int class_create_file(struct class *cls, const struct class_attribute *attr)
{
int error;
if (cls) {
if (cls)
error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
} else
else
error = -EINVAL;
return error;
}
void class_remove_file(struct class * cls, const struct class_attribute * attr)
void class_remove_file(struct class *cls, const struct class_attribute *attr)
{
if (cls)
sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
@ -93,48 +93,48 @@ void class_remove_file(struct class * cls, const struct class_attribute * attr)
static struct class *class_get(struct class *cls)
{
if (cls)
return container_of(kset_get(&cls->subsys), struct class, subsys);
return container_of(kset_get(&cls->subsys),
struct class, subsys);
return NULL;
}
static void class_put(struct class * cls)
static void class_put(struct class *cls)
{
if (cls)
kset_put(&cls->subsys);
}
static int add_class_attrs(struct class * cls)
static int add_class_attrs(struct class *cls)
{
int i;
int error = 0;
if (cls->class_attrs) {
for (i = 0; attr_name(cls->class_attrs[i]); i++) {
error = class_create_file(cls,&cls->class_attrs[i]);
error = class_create_file(cls, &cls->class_attrs[i]);
if (error)
goto Err;
goto error;
}
}
Done:
done:
return error;
Err:
error:
while (--i >= 0)
class_remove_file(cls,&cls->class_attrs[i]);
goto Done;
class_remove_file(cls, &cls->class_attrs[i]);
goto done;
}
static void remove_class_attrs(struct class * cls)
static void remove_class_attrs(struct class *cls)
{
int i;
if (cls->class_attrs) {
for (i = 0; attr_name(cls->class_attrs[i]); i++)
class_remove_file(cls,&cls->class_attrs[i]);
class_remove_file(cls, &cls->class_attrs[i]);
}
}
int class_register(struct class * cls)
int class_register(struct class *cls)
{
int error;
@ -149,9 +149,16 @@ int class_register(struct class * cls)
if (error)
return error;
cls->subsys.kobj.kset = &class_subsys;
#ifdef CONFIG_SYSFS_DEPRECATED
/* let the block class directory show up in the root of sysfs */
if (cls != &block_class)
cls->subsys.kobj.kset = class_kset;
#else
cls->subsys.kobj.kset = class_kset;
#endif
cls->subsys.kobj.ktype = &class_ktype;
error = subsystem_register(&cls->subsys);
error = kset_register(&cls->subsys);
if (!error) {
error = add_class_attrs(class_get(cls));
class_put(cls);
@ -159,11 +166,11 @@ int class_register(struct class * cls)
return error;
}
void class_unregister(struct class * cls)
void class_unregister(struct class *cls)
{
pr_debug("device class '%s': unregistering\n", cls->name);
remove_class_attrs(cls);
subsystem_unregister(&cls->subsys);
kset_unregister(&cls->subsys);
}
static void class_create_release(struct class *cls)
@ -241,8 +248,8 @@ void class_destroy(struct class *cls)
/* Class Device Stuff */
int class_device_create_file(struct class_device * class_dev,
const struct class_device_attribute * attr)
int class_device_create_file(struct class_device *class_dev,
const struct class_device_attribute *attr)
{
int error = -EINVAL;
if (class_dev)
@ -250,8 +257,8 @@ int class_device_create_file(struct class_device * class_dev,
return error;
}
void class_device_remove_file(struct class_device * class_dev,
const struct class_device_attribute * attr)
void class_device_remove_file(struct class_device *class_dev,
const struct class_device_attribute *attr)
{
if (class_dev)
sysfs_remove_file(&class_dev->kobj, &attr->attr);
@ -273,12 +280,11 @@ void class_device_remove_bin_file(struct class_device *class_dev,
sysfs_remove_bin_file(&class_dev->kobj, attr);
}
static ssize_t
class_device_attr_show(struct kobject * kobj, struct attribute * attr,
char * buf)
static ssize_t class_device_attr_show(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
struct class_device * cd = to_class_dev(kobj);
struct class_device_attribute *class_dev_attr = to_class_dev_attr(attr);
struct class_device *cd = to_class_dev(kobj);
ssize_t ret = 0;
if (class_dev_attr->show)
@ -286,12 +292,12 @@ class_device_attr_show(struct kobject * kobj, struct attribute * attr,
return ret;
}
static ssize_t
class_device_attr_store(struct kobject * kobj, struct attribute * attr,
const char * buf, size_t count)
static ssize_t class_device_attr_store(struct kobject *kobj,
struct attribute *attr,
const char *buf, size_t count)
{
struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
struct class_device * cd = to_class_dev(kobj);
struct class_device_attribute *class_dev_attr = to_class_dev_attr(attr);
struct class_device *cd = to_class_dev(kobj);
ssize_t ret = 0;
if (class_dev_attr->store)
@ -304,10 +310,10 @@ static struct sysfs_ops class_dev_sysfs_ops = {
.store = class_device_attr_store,
};
static void class_dev_release(struct kobject * kobj)
static void class_dev_release(struct kobject *kobj)
{
struct class_device *cd = to_class_dev(kobj);
struct class * cls = cd->class;
struct class *cls = cd->class;
pr_debug("device class '%s': release.\n", cd->class_id);
@ -316,8 +322,8 @@ static void class_dev_release(struct kobject * kobj)
else if (cls->release)
cls->release(cd);
else {
printk(KERN_ERR "Class Device '%s' does not have a release() function, "
"it is broken and must be fixed.\n",
printk(KERN_ERR "Class Device '%s' does not have a release() "
"function, it is broken and must be fixed.\n",
cd->class_id);
WARN_ON(1);
}
@ -428,7 +434,8 @@ static int class_uevent(struct kset *kset, struct kobject *kobj,
add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
if (dev->driver)
add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
add_uevent_var(env, "PHYSDEVDRIVER=%s",
dev->driver->name);
}
if (class_dev->uevent) {
@ -452,43 +459,49 @@ static struct kset_uevent_ops class_uevent_ops = {
.uevent = class_uevent,
};
static decl_subsys(class_obj, &class_device_ktype, &class_uevent_ops);
/*
* DO NOT copy how this is created, kset_create_and_add() should be
* called, but this is a hold-over from the old-way and will be deleted
* entirely soon.
*/
static struct kset class_obj_subsys = {
.uevent_ops = &class_uevent_ops,
};
static int class_device_add_attrs(struct class_device * cd)
static int class_device_add_attrs(struct class_device *cd)
{
int i;
int error = 0;
struct class * cls = cd->class;
struct class *cls = cd->class;
if (cls->class_dev_attrs) {
for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
error = class_device_create_file(cd,
&cls->class_dev_attrs[i]);
&cls->class_dev_attrs[i]);
if (error)
goto Err;
goto err;
}
}
Done:
done:
return error;
Err:
err:
while (--i >= 0)
class_device_remove_file(cd,&cls->class_dev_attrs[i]);
goto Done;
class_device_remove_file(cd, &cls->class_dev_attrs[i]);
goto done;
}
static void class_device_remove_attrs(struct class_device * cd)
static void class_device_remove_attrs(struct class_device *cd)
{
int i;
struct class * cls = cd->class;
struct class *cls = cd->class;
if (cls->class_dev_attrs) {
for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
class_device_remove_file(cd,&cls->class_dev_attrs[i]);
class_device_remove_file(cd, &cls->class_dev_attrs[i]);
}
}
static int class_device_add_groups(struct class_device * cd)
static int class_device_add_groups(struct class_device *cd)
{
int i;
int error = 0;
@ -498,7 +511,8 @@ static int class_device_add_groups(struct class_device * cd)
error = sysfs_create_group(&cd->kobj, cd->groups[i]);
if (error) {
while (--i >= 0)
sysfs_remove_group(&cd->kobj, cd->groups[i]);
sysfs_remove_group(&cd->kobj,
cd->groups[i]);
goto out;
}
}
@ -507,14 +521,12 @@ static int class_device_add_groups(struct class_device * cd)
return error;
}
static void class_device_remove_groups(struct class_device * cd)
static void class_device_remove_groups(struct class_device *cd)
{
int i;
if (cd->groups) {
for (i = 0; cd->groups[i]; i++) {
if (cd->groups)
for (i = 0; cd->groups[i]; i++)
sysfs_remove_group(&cd->kobj, cd->groups[i]);
}
}
}
static ssize_t show_dev(struct class_device *class_dev, char *buf)
@ -537,8 +549,8 @@ static struct class_device_attribute class_uevent_attr =
void class_device_initialize(struct class_device *class_dev)
{
kobj_set_kset_s(class_dev, class_obj_subsys);
kobject_init(&class_dev->kobj);
class_dev->kobj.kset = &class_obj_subsys;
kobject_init(&class_dev->kobj, &class_device_ktype);
INIT_LIST_HEAD(&class_dev->node);
}
@ -566,16 +578,13 @@ int class_device_add(struct class_device *class_dev)
class_dev->class_id);
/* first, register with generic layer. */
error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
if (error)
goto out2;
if (parent_class_dev)
class_dev->kobj.parent = &parent_class_dev->kobj;
else
class_dev->kobj.parent = &parent_class->subsys.kobj;
error = kobject_add(&class_dev->kobj);
error = kobject_add(&class_dev->kobj, class_dev->kobj.parent,
"%s", class_dev->class_id);
if (error)
goto out2;
@ -642,7 +651,7 @@ int class_device_add(struct class_device *class_dev)
out3:
kobject_del(&class_dev->kobj);
out2:
if(parent_class_dev)
if (parent_class_dev)
class_device_put(parent_class_dev);
class_put(parent_class);
out1:
@ -659,9 +668,11 @@ int class_device_register(struct class_device *class_dev)
/**
* class_device_create - creates a class device and registers it with sysfs
* @cls: pointer to the struct class that this device should be registered to.
* @parent: pointer to the parent struct class_device of this new device, if any.
* @parent: pointer to the parent struct class_device of this new device, if
* any.
* @devt: the dev_t for the char device to be added.
* @device: a pointer to a struct device that is assiociated with this class device.
* @device: a pointer to a struct device that is assiociated with this class
* device.
* @fmt: string for the class device's name
*
* 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)
class_device_unregister(class_dev);
}
struct class_device * class_device_get(struct class_device *class_dev)
struct class_device *class_device_get(struct class_device *class_dev)
{
if (class_dev)
return to_class_dev(kobject_get(&class_dev->kobj));
@ -798,6 +809,139 @@ void class_device_put(struct class_device *class_dev)
kobject_put(&class_dev->kobj);
}
/**
* class_for_each_device - device iterator
* @class: the class we're iterating
* @data: data for the callback
* @fn: function to be called for each device
*
* Iterate over @class's list of devices, and call @fn for each,
* passing it @data.
*
* We check the return of @fn each time. If it returns anything
* other than 0, we break out and return that value.
*
* Note, we hold class->sem in this function, so it can not be
* re-acquired in @fn, otherwise it will self-deadlocking. For
* example, calls to add or remove class members would be verboten.
*/
int class_for_each_device(struct class *class, void *data,
int (*fn)(struct device *, void *))
{
struct device *dev;
int error = 0;
if (!class)
return -EINVAL;
down(&class->sem);
list_for_each_entry(dev, &class->devices, node) {
dev = get_device(dev);
if (dev) {
error = fn(dev, data);
put_device(dev);
} else
error = -ENODEV;
if (error)
break;
}
up(&class->sem);
return error;
}
EXPORT_SYMBOL_GPL(class_for_each_device);
/**
* class_find_device - device iterator for locating a particular device
* @class: the class we're iterating
* @data: data for the match function
* @match: function to check device
*
* This is similar to the class_for_each_dev() function above, but it
* returns a reference to a device that is 'found' for later use, as
* determined by the @match callback.
*
* The callback should return 0 if the device doesn't match and non-zero
* if it does. If the callback returns non-zero, this function will
* return to the caller and not iterate over any more devices.
* Note, you will need to drop the reference with put_device() after use.
*
* We hold class->sem in this function, so it can not be
* re-acquired in @match, otherwise it will self-deadlocking. For
* example, calls to add or remove class members would be verboten.
*/
struct device *class_find_device(struct class *class, void *data,
int (*match)(struct device *, void *))
{
struct device *dev;
int found = 0;
if (!class)
return NULL;
down(&class->sem);
list_for_each_entry(dev, &class->devices, node) {
dev = get_device(dev);
if (dev) {
if (match(dev, data)) {
found = 1;
break;
} else
put_device(dev);
} else
break;
}
up(&class->sem);
return found ? dev : NULL;
}
EXPORT_SYMBOL_GPL(class_find_device);
/**
* class_find_child - device iterator for locating a particular class_device
* @class: the class we're iterating
* @data: data for the match function
* @match: function to check class_device
*
* This function returns a reference to a class_device that is 'found' for
* later use, as determined by the @match callback.
*
* The callback should return 0 if the class_device doesn't match and non-zero
* if it does. If the callback returns non-zero, this function will
* return to the caller and not iterate over any more class_devices.
*
* Note, you will need to drop the reference with class_device_put() after use.
*
* We hold class->sem in this function, so it can not be
* re-acquired in @match, otherwise it will self-deadlocking. For
* example, calls to add or remove class members would be verboten.
*/
struct class_device *class_find_child(struct class *class, void *data,
int (*match)(struct class_device *, void *))
{
struct class_device *dev;
int found = 0;
if (!class)
return NULL;
down(&class->sem);
list_for_each_entry(dev, &class->children, node) {
dev = class_device_get(dev);
if (dev) {
if (match(dev, data)) {
found = 1;
break;
} else
class_device_put(dev);
} else
break;
}
up(&class->sem);
return found ? dev : NULL;
}
EXPORT_SYMBOL_GPL(class_find_child);
int class_interface_register(struct class_interface *class_intf)
{
@ -829,7 +973,7 @@ int class_interface_register(struct class_interface *class_intf)
void class_interface_unregister(struct class_interface *class_intf)
{
struct class * parent = class_intf->class;
struct class *parent = class_intf->class;
struct class_device *class_dev;
struct device *dev;
@ -853,15 +997,14 @@ void class_interface_unregister(struct class_interface *class_intf)
int __init classes_init(void)
{
int retval;
retval = subsystem_register(&class_subsys);
if (retval)
return retval;
class_kset = kset_create_and_add("class", NULL, NULL);
if (!class_kset)
return -ENOMEM;
/* ick, this is ugly, the things we go through to keep from showing up
* in sysfs... */
kset_init(&class_obj_subsys);
kobject_set_name(&class_obj_subsys.kobj, "class_obj");
if (!class_obj_subsys.kobj.parent)
class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
return 0;

View file

@ -18,14 +18,14 @@
#include <linux/string.h>
#include <linux/kdev_t.h>
#include <linux/notifier.h>
#include <linux/genhd.h>
#include <asm/semaphore.h>
#include "base.h"
#include "power/power.h"
int (*platform_notify)(struct device * dev) = NULL;
int (*platform_notify_remove)(struct device * dev) = NULL;
int (*platform_notify)(struct device *dev) = NULL;
int (*platform_notify_remove)(struct device *dev) = NULL;
/*
* sysfs bindings for devices.
@ -51,11 +51,11 @@ EXPORT_SYMBOL(dev_driver_string);
#define to_dev(obj) container_of(obj, struct device, kobj)
#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
static ssize_t
dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct device_attribute * dev_attr = to_dev_attr(attr);
struct device * dev = to_dev(kobj);
struct device_attribute *dev_attr = to_dev_attr(attr);
struct device *dev = to_dev(kobj);
ssize_t ret = -EIO;
if (dev_attr->show)
@ -63,12 +63,11 @@ dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
return ret;
}
static ssize_t
dev_attr_store(struct kobject * kobj, struct attribute * attr,
const char * buf, size_t count)
static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
struct device_attribute * dev_attr = to_dev_attr(attr);
struct device * dev = to_dev(kobj);
struct device_attribute *dev_attr = to_dev_attr(attr);
struct device *dev = to_dev(kobj);
ssize_t ret = -EIO;
if (dev_attr->store)
@ -90,9 +89,9 @@ static struct sysfs_ops dev_sysfs_ops = {
* reaches 0. We forward the call to the device's release
* method, which should handle actually freeing the structure.
*/
static void device_release(struct kobject * kobj)
static void device_release(struct kobject *kobj)
{
struct device * dev = to_dev(kobj);
struct device *dev = to_dev(kobj);
if (dev->release)
dev->release(dev);
@ -101,8 +100,8 @@ static void device_release(struct kobject * kobj)
else if (dev->class && dev->class->dev_release)
dev->class->dev_release(dev);
else {
printk(KERN_ERR "Device '%s' does not have a release() function, "
"it is broken and must be fixed.\n",
printk(KERN_ERR "Device '%s' does not have a release() "
"function, it is broken and must be fixed.\n",
dev->bus_id);
WARN_ON(1);
}
@ -185,7 +184,8 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
if (dev->driver)
add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
add_uevent_var(env, "PHYSDEVDRIVER=%s",
dev->driver->name);
}
#endif
@ -193,15 +193,16 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
if (dev->bus && dev->bus->uevent) {
retval = dev->bus->uevent(dev, env);
if (retval)
pr_debug ("%s: bus uevent() returned %d\n",
__FUNCTION__, retval);
pr_debug("device: '%s': %s: bus uevent() returned %d\n",
dev->bus_id, __FUNCTION__, retval);
}
/* have the class specific function add its stuff */
if (dev->class && dev->class->dev_uevent) {
retval = dev->class->dev_uevent(dev, env);
if (retval)
pr_debug("%s: class uevent() returned %d\n",
pr_debug("device: '%s': %s: class uevent() "
"returned %d\n", dev->bus_id,
__FUNCTION__, retval);
}
@ -209,7 +210,8 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
if (dev->type && dev->type->uevent) {
retval = dev->type->uevent(dev, env);
if (retval)
pr_debug("%s: dev_type uevent() returned %d\n",
pr_debug("device: '%s': %s: dev_type uevent() "
"returned %d\n", dev->bus_id,
__FUNCTION__, retval);
}
@ -325,7 +327,8 @@ static int device_add_groups(struct device *dev,
error = sysfs_create_group(&dev->kobj, groups[i]);
if (error) {
while (--i >= 0)
sysfs_remove_group(&dev->kobj, groups[i]);
sysfs_remove_group(&dev->kobj,
groups[i]);
break;
}
}
@ -401,20 +404,15 @@ static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
static struct device_attribute devt_attr =
__ATTR(dev, S_IRUGO, show_dev, NULL);
/*
* devices_subsys - structure to be registered with kobject core.
*/
decl_subsys(devices, &device_ktype, &device_uevent_ops);
/* kset to create /sys/devices/ */
struct kset *devices_kset;
/**
* device_create_file - create sysfs attribute file for device.
* @dev: device.
* @attr: device attribute descriptor.
* device_create_file - create sysfs attribute file for device.
* @dev: device.
* @attr: device attribute descriptor.
*/
int device_create_file(struct device * dev, struct device_attribute * attr)
int device_create_file(struct device *dev, struct device_attribute *attr)
{
int error = 0;
if (get_device(dev)) {
@ -425,12 +423,11 @@ int device_create_file(struct device * dev, struct device_attribute * attr)
}
/**
* device_remove_file - remove sysfs attribute file.
* @dev: device.
* @attr: device attribute descriptor.
* device_remove_file - remove sysfs attribute file.
* @dev: device.
* @attr: device attribute descriptor.
*/
void device_remove_file(struct device * dev, struct device_attribute * attr)
void device_remove_file(struct device *dev, struct device_attribute *attr)
{
if (get_device(dev)) {
sysfs_remove_file(&dev->kobj, &attr->attr);
@ -511,22 +508,20 @@ static void klist_children_put(struct klist_node *n)
put_device(dev);
}
/**
* device_initialize - init device structure.
* @dev: device.
* device_initialize - init device structure.
* @dev: device.
*
* This prepares the device for use by other layers,
* including adding it to the device hierarchy.
* It is the first half of device_register(), if called by
* that, though it can also be called separately, so one
* may use @dev's fields (e.g. the refcount).
* This prepares the device for use by other layers,
* including adding it to the device hierarchy.
* It is the first half of device_register(), if called by
* that, though it can also be called separately, so one
* may use @dev's fields (e.g. the refcount).
*/
void device_initialize(struct device *dev)
{
kobj_set_kset_s(dev, devices_subsys);
kobject_init(&dev->kobj);
dev->kobj.kset = devices_kset;
kobject_init(&dev->kobj, &device_ktype);
klist_init(&dev->klist_children, klist_children_get,
klist_children_put);
INIT_LIST_HEAD(&dev->dma_pools);
@ -539,36 +534,39 @@ void device_initialize(struct device *dev)
}
#ifdef CONFIG_SYSFS_DEPRECATED
static struct kobject * get_device_parent(struct device *dev,
struct device *parent)
static struct kobject *get_device_parent(struct device *dev,
struct device *parent)
{
/*
* Set the parent to the class, not the parent device
* for topmost devices in class hierarchy.
* This keeps sysfs from having a symlink to make old
* udevs happy
*/
/* class devices without a parent live in /sys/class/<classname>/ */
if (dev->class && (!parent || parent->class != dev->class))
return &dev->class->subsys.kobj;
/* all other devices keep their parent */
else if (parent)
return &parent->kobj;
return NULL;
}
static inline void cleanup_device_parent(struct device *dev) {}
static inline void cleanup_glue_dir(struct device *dev,
struct kobject *glue_dir) {}
#else
static struct kobject *virtual_device_parent(struct device *dev)
{
static struct kobject *virtual_dir = NULL;
if (!virtual_dir)
virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual");
virtual_dir = kobject_create_and_add("virtual",
&devices_kset->kobj);
return virtual_dir;
}
static struct kobject * get_device_parent(struct device *dev,
struct device *parent)
static struct kobject *get_device_parent(struct device *dev,
struct device *parent)
{
int retval;
if (dev->class) {
struct kobject *kobj = NULL;
struct kobject *parent_kobj;
@ -576,8 +574,8 @@ static struct kobject * get_device_parent(struct device *dev,
/*
* If we have no parent, we live in "virtual".
* Class-devices with a bus-device as parent, live
* in a class-directory to prevent namespace collisions.
* Class-devices with a non class-device as parent, live
* in a "glue" directory to prevent namespace collisions.
*/
if (parent == NULL)
parent_kobj = virtual_device_parent(dev);
@ -598,25 +596,45 @@ static struct kobject * get_device_parent(struct device *dev,
return kobj;
/* or create a new class-directory at the parent device */
return kobject_kset_add_dir(&dev->class->class_dirs,
parent_kobj, dev->class->name);
k = kobject_create();
if (!k)
return NULL;
k->kset = &dev->class->class_dirs;
retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
if (retval < 0) {
kobject_put(k);
return NULL;
}
/* do not emit an uevent for this simple "glue" directory */
return k;
}
if (parent)
return &parent->kobj;
return NULL;
}
static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
{
/* see if we live in a "glue" directory */
if (!dev->class || glue_dir->kset != &dev->class->class_dirs)
return;
kobject_put(glue_dir);
}
static void cleanup_device_parent(struct device *dev)
{
cleanup_glue_dir(dev, dev->kobj.parent);
}
#endif
static int setup_parent(struct device *dev, struct device *parent)
static void setup_parent(struct device *dev, struct device *parent)
{
struct kobject *kobj;
kobj = get_device_parent(dev, parent);
if (IS_ERR(kobj))
return PTR_ERR(kobj);
if (kobj)
dev->kobj.parent = kobj;
return 0;
}
static int device_add_class_symlinks(struct device *dev)
@ -625,65 +643,76 @@ static int device_add_class_symlinks(struct device *dev)
if (!dev->class)
return 0;
error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
"subsystem");
if (error)
goto out;
/*
* If this is not a "fake" compatible device, then create the
* symlink from the class to the device.
*/
if (dev->kobj.parent != &dev->class->subsys.kobj) {
#ifdef CONFIG_SYSFS_DEPRECATED
/* stacked class devices need a symlink in the class directory */
if (dev->kobj.parent != &dev->class->subsys.kobj &&
dev->type != &part_type) {
error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
dev->bus_id);
if (error)
goto out_subsys;
}
if (dev->parent) {
#ifdef CONFIG_SYSFS_DEPRECATED
{
struct device *parent = dev->parent;
char *class_name;
/*
* In old sysfs stacked class devices had 'device'
* link pointing to real device instead of parent
*/
while (parent->class && !parent->bus && parent->parent)
parent = parent->parent;
if (dev->parent && dev->type != &part_type) {
struct device *parent = dev->parent;
char *class_name;
error = sysfs_create_link(&dev->kobj,
&parent->kobj,
"device");
if (error)
goto out_busid;
/*
* stacked class devices have the 'device' link
* pointing to the bus device instead of the parent
*/
while (parent->class && !parent->bus && parent->parent)
parent = parent->parent;
class_name = make_class_name(dev->class->name,
&dev->kobj);
if (class_name)
error = sysfs_create_link(&dev->parent->kobj,
&dev->kobj, class_name);
kfree(class_name);
if (error)
goto out_device;
}
error = sysfs_create_link(&dev->kobj,
&parent->kobj,
"device");
if (error)
goto out_busid;
class_name = make_class_name(dev->class->name,
&dev->kobj);
if (class_name)
error = sysfs_create_link(&dev->parent->kobj,
&dev->kobj, class_name);
kfree(class_name);
if (error)
goto out_device;
}
return 0;
out_device:
if (dev->parent && dev->type != &part_type)
sysfs_remove_link(&dev->kobj, "device");
out_busid:
if (dev->kobj.parent != &dev->class->subsys.kobj &&
dev->type != &part_type)
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
#else
/* link in the class directory pointing to the device */
error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
dev->bus_id);
if (error)
goto out_subsys;
if (dev->parent && dev->type != &part_type) {
error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
"device");
if (error)
goto out_busid;
#endif
}
return 0;
#ifdef CONFIG_SYSFS_DEPRECATED
out_device:
if (dev->parent)
sysfs_remove_link(&dev->kobj, "device");
#endif
out_busid:
if (dev->kobj.parent != &dev->class->subsys.kobj)
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
#endif
out_subsys:
sysfs_remove_link(&dev->kobj, "subsystem");
out:
@ -694,8 +723,9 @@ static void device_remove_class_symlinks(struct device *dev)
{
if (!dev->class)
return;
if (dev->parent) {
#ifdef CONFIG_SYSFS_DEPRECATED
if (dev->parent && dev->type != &part_type) {
char *class_name;
class_name = make_class_name(dev->class->name, &dev->kobj);
@ -703,45 +733,59 @@ static void device_remove_class_symlinks(struct device *dev)
sysfs_remove_link(&dev->parent->kobj, class_name);
kfree(class_name);
}
#endif
sysfs_remove_link(&dev->kobj, "device");
}
if (dev->kobj.parent != &dev->class->subsys.kobj)
if (dev->kobj.parent != &dev->class->subsys.kobj &&
dev->type != &part_type)
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
#else
if (dev->parent && dev->type != &part_type)
sysfs_remove_link(&dev->kobj, "device");
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
#endif
sysfs_remove_link(&dev->kobj, "subsystem");
}
/**
* device_add - add device to device hierarchy.
* @dev: device.
* device_add - add device to device hierarchy.
* @dev: device.
*
* This is part 2 of device_register(), though may be called
* separately _iff_ device_initialize() has been called separately.
* This is part 2 of device_register(), though may be called
* separately _iff_ device_initialize() has been called separately.
*
* This adds it to the kobject hierarchy via kobject_add(), adds it
* to the global and sibling lists for the device, then
* adds it to the other relevant subsystems of the driver model.
* This adds it to the kobject hierarchy via kobject_add(), adds it
* to the global and sibling lists for the device, then
* adds it to the other relevant subsystems of the driver model.
*/
int device_add(struct device *dev)
{
struct device *parent = NULL;
struct class_interface *class_intf;
int error = -EINVAL;
int error;
error = pm_sleep_lock();
if (error) {
dev_warn(dev, "Suspicious %s during suspend\n", __FUNCTION__);
dump_stack();
return error;
}
dev = get_device(dev);
if (!dev || !strlen(dev->bus_id))
if (!dev || !strlen(dev->bus_id)) {
error = -EINVAL;
goto Error;
}
pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
parent = get_device(dev->parent);
error = setup_parent(dev, parent);
if (error)
goto Error;
setup_parent(dev, parent);
/* first, register with generic layer. */
kobject_set_name(&dev->kobj, "%s", dev->bus_id);
error = kobject_add(&dev->kobj);
error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
if (error)
goto Error;
@ -751,7 +795,7 @@ int device_add(struct device *dev)
/* notify clients of device entry (new way) */
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->bus_notifier,
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
BUS_NOTIFY_ADD_DEVICE, dev);
error = device_create_file(dev, &uevent_attr);
@ -795,13 +839,14 @@ int device_add(struct device *dev)
}
Done:
put_device(dev);
pm_sleep_unlock();
return error;
BusError:
device_pm_remove(dev);
dpm_sysfs_remove(dev);
PMError:
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->bus_notifier,
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
BUS_NOTIFY_DEL_DEVICE, dev);
device_remove_attrs(dev);
AttrsError:
@ -809,124 +854,84 @@ int device_add(struct device *dev)
SymlinkError:
if (MAJOR(dev->devt))
device_remove_file(dev, &devt_attr);
if (dev->class) {
sysfs_remove_link(&dev->kobj, "subsystem");
/* If this is not a "fake" compatible device, remove the
* symlink from the class to the device. */
if (dev->kobj.parent != &dev->class->subsys.kobj)
sysfs_remove_link(&dev->class->subsys.kobj,
dev->bus_id);
if (parent) {
#ifdef CONFIG_SYSFS_DEPRECATED
char *class_name = make_class_name(dev->class->name,
&dev->kobj);
if (class_name)
sysfs_remove_link(&dev->parent->kobj,
class_name);
kfree(class_name);
#endif
sysfs_remove_link(&dev->kobj, "device");
}
}
ueventattrError:
device_remove_file(dev, &uevent_attr);
attrError:
kobject_uevent(&dev->kobj, KOBJ_REMOVE);
kobject_del(&dev->kobj);
Error:
cleanup_device_parent(dev);
if (parent)
put_device(parent);
goto Done;
}
/**
* device_register - register a device with the system.
* @dev: pointer to the device structure
* device_register - register a device with the system.
* @dev: pointer to the device structure
*
* This happens in two clean steps - initialize the device
* and add it to the system. The two steps can be called
* separately, but this is the easiest and most common.
* I.e. you should only call the two helpers separately if
* have a clearly defined need to use and refcount the device
* before it is added to the hierarchy.
* This happens in two clean steps - initialize the device
* and add it to the system. The two steps can be called
* separately, but this is the easiest and most common.
* I.e. you should only call the two helpers separately if
* have a clearly defined need to use and refcount the device
* before it is added to the hierarchy.
*/
int device_register(struct device *dev)
{
device_initialize(dev);
return device_add(dev);
}
/**
* get_device - increment reference count for device.
* @dev: device.
* get_device - increment reference count for device.
* @dev: device.
*
* This simply forwards the call to kobject_get(), though
* we do take care to provide for the case that we get a NULL
* pointer passed in.
* This simply forwards the call to kobject_get(), though
* we do take care to provide for the case that we get a NULL
* pointer passed in.
*/
struct device * get_device(struct device * dev)
struct device *get_device(struct device *dev)
{
return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
}
/**
* put_device - decrement reference count.
* @dev: device in question.
* put_device - decrement reference count.
* @dev: device in question.
*/
void put_device(struct device * dev)
void put_device(struct device *dev)
{
/* might_sleep(); */
if (dev)
kobject_put(&dev->kobj);
}
/**
* device_del - delete device from system.
* @dev: device.
* device_del - delete device from system.
* @dev: device.
*
* This is the first part of the device unregistration
* sequence. This removes the device from the lists we control
* from here, has it removed from the other driver model
* subsystems it was added to in device_add(), and removes it
* from the kobject hierarchy.
* This is the first part of the device unregistration
* sequence. This removes the device from the lists we control
* from here, has it removed from the other driver model
* subsystems it was added to in device_add(), and removes it
* from the kobject hierarchy.
*
* NOTE: this should be called manually _iff_ device_add() was
* also called manually.
* NOTE: this should be called manually _iff_ device_add() was
* also called manually.
*/
void device_del(struct device * dev)
void device_del(struct device *dev)
{
struct device * parent = dev->parent;
struct device *parent = dev->parent;
struct class_interface *class_intf;
device_pm_remove(dev);
if (parent)
klist_del(&dev->knode_parent);
if (MAJOR(dev->devt))
device_remove_file(dev, &devt_attr);
if (dev->class) {
sysfs_remove_link(&dev->kobj, "subsystem");
/* If this is not a "fake" compatible device, remove the
* symlink from the class to the device. */
if (dev->kobj.parent != &dev->class->subsys.kobj)
sysfs_remove_link(&dev->class->subsys.kobj,
dev->bus_id);
if (parent) {
#ifdef CONFIG_SYSFS_DEPRECATED
char *class_name = make_class_name(dev->class->name,
&dev->kobj);
if (class_name)
sysfs_remove_link(&dev->parent->kobj,
class_name);
kfree(class_name);
#endif
sysfs_remove_link(&dev->kobj, "device");
}
device_remove_class_symlinks(dev);
down(&dev->class->sem);
/* notify any interfaces that the device is now gone */
@ -936,31 +941,6 @@ void device_del(struct device * dev)
/* remove the device from the class list */
list_del_init(&dev->node);
up(&dev->class->sem);
/* If we live in a parent class-directory, unreference it */
if (dev->kobj.parent->kset == &dev->class->class_dirs) {
struct device *d;
int other = 0;
/*
* if we are the last child of our class, delete
* our class-directory at this parent
*/
down(&dev->class->sem);
list_for_each_entry(d, &dev->class->devices, node) {
if (d == dev)
continue;
if (d->kobj.parent == dev->kobj.parent) {
other = 1;
break;
}
}
if (!other)
kobject_del(dev->kobj.parent);
kobject_put(dev->kobj.parent);
up(&dev->class->sem);
}
}
device_remove_file(dev, &uevent_attr);
device_remove_attrs(dev);
@ -979,57 +959,55 @@ void device_del(struct device * dev)
if (platform_notify_remove)
platform_notify_remove(dev);
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->bus_notifier,
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
BUS_NOTIFY_DEL_DEVICE, dev);
device_pm_remove(dev);
kobject_uevent(&dev->kobj, KOBJ_REMOVE);
cleanup_device_parent(dev);
kobject_del(&dev->kobj);
if (parent)
put_device(parent);
put_device(parent);
}
/**
* device_unregister - unregister device from system.
* @dev: device going away.
* device_unregister - unregister device from system.
* @dev: device going away.
*
* We do this in two parts, like we do device_register(). First,
* we remove it from all the subsystems with device_del(), then
* we decrement the reference count via put_device(). If that
* is the final reference count, the device will be cleaned up
* via device_release() above. Otherwise, the structure will
* stick around until the final reference to the device is dropped.
* We do this in two parts, like we do device_register(). First,
* we remove it from all the subsystems with device_del(), then
* we decrement the reference count via put_device(). If that
* is the final reference count, the device will be cleaned up
* via device_release() above. Otherwise, the structure will
* stick around until the final reference to the device is dropped.
*/
void device_unregister(struct device * dev)
void device_unregister(struct device *dev)
{
pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
device_del(dev);
put_device(dev);
}
static struct device * next_device(struct klist_iter * i)
static struct device *next_device(struct klist_iter *i)
{
struct klist_node * n = klist_next(i);
struct klist_node *n = klist_next(i);
return n ? container_of(n, struct device, knode_parent) : NULL;
}
/**
* device_for_each_child - device child iterator.
* @parent: parent struct device.
* @data: data for the callback.
* @fn: function to be called for each device.
* device_for_each_child - device child iterator.
* @parent: parent struct device.
* @data: data for the callback.
* @fn: function to be called for each device.
*
* Iterate over @parent's child devices, and call @fn for each,
* passing it @data.
* Iterate over @parent's child devices, and call @fn for each,
* passing it @data.
*
* We check the return of @fn each time. If it returns anything
* other than 0, we break out and return that value.
* We check the return of @fn each time. If it returns anything
* other than 0, we break out and return that value.
*/
int device_for_each_child(struct device * parent, void * data,
int (*fn)(struct device *, void *))
int device_for_each_child(struct device *parent, void *data,
int (*fn)(struct device *dev, void *data))
{
struct klist_iter i;
struct device * child;
struct device *child;
int error = 0;
klist_iter_init(&parent->klist_children, &i);
@ -1054,8 +1032,8 @@ int device_for_each_child(struct device * parent, void * data,
* current device can be obtained, this function will return to the caller
* and not iterate over any more devices.
*/
struct device * device_find_child(struct device *parent, void *data,
int (*match)(struct device *, void *))
struct device *device_find_child(struct device *parent, void *data,
int (*match)(struct device *dev, void *data))
{
struct klist_iter i;
struct device *child;
@ -1073,7 +1051,10 @@ struct device * device_find_child(struct device *parent, void *data,
int __init devices_init(void)
{
return subsystem_register(&devices_subsys);
devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
if (!devices_kset)
return -ENOMEM;
return 0;
}
EXPORT_SYMBOL_GPL(device_for_each_child);
@ -1094,7 +1075,7 @@ EXPORT_SYMBOL_GPL(device_remove_file);
static void device_create_release(struct device *dev)
{
pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
kfree(dev);
}
@ -1156,14 +1137,11 @@ struct device *device_create(struct class *class, struct device *parent,
EXPORT_SYMBOL_GPL(device_create);
/**
* device_destroy - removes a device that was created with device_create()
* find_device - finds a device that was created with device_create()
* @class: pointer to the struct class that this device was registered with
* @devt: the dev_t of the device that was previously registered
*
* This call unregisters and cleans up a device that was created with a
* call to device_create().
*/
void device_destroy(struct class *class, dev_t devt)
static struct device *find_device(struct class *class, dev_t devt)
{
struct device *dev = NULL;
struct device *dev_tmp;
@ -1176,12 +1154,54 @@ void device_destroy(struct class *class, dev_t devt)
}
}
up(&class->sem);
return dev;
}
/**
* device_destroy - removes a device that was created with device_create()
* @class: pointer to the struct class that this device was registered with
* @devt: the dev_t of the device that was previously registered
*
* This call unregisters and cleans up a device that was created with a
* call to device_create().
*/
void device_destroy(struct class *class, dev_t devt)
{
struct device *dev;
dev = find_device(class, devt);
if (dev)
device_unregister(dev);
}
EXPORT_SYMBOL_GPL(device_destroy);
#ifdef CONFIG_PM_SLEEP
/**
* destroy_suspended_device - asks the PM core to remove a suspended device
* @class: pointer to the struct class that this device was registered with
* @devt: the dev_t of the device that was previously registered
*
* This call notifies the PM core of the necessity to unregister a suspended
* device created with a call to device_create() (devices cannot be
* unregistered directly while suspended, since the PM core holds their
* semaphores at that time).
*
* It can only be called within the scope of a system sleep transition. In
* practice this means it has to be directly or indirectly invoked either by
* a suspend or resume method, or by the PM core (e.g. via
* disable_nonboot_cpus() or enable_nonboot_cpus()).
*/
void destroy_suspended_device(struct class *class, dev_t devt)
{
struct device *dev;
dev = find_device(class, devt);
if (dev)
device_pm_schedule_removal(dev);
}
EXPORT_SYMBOL_GPL(destroy_suspended_device);
#endif /* CONFIG_PM_SLEEP */
/**
* device_rename - renames a device
* @dev: the pointer to the struct device to be renamed
@ -1198,7 +1218,8 @@ int device_rename(struct device *dev, char *new_name)
if (!dev)
return -EINVAL;
pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
__FUNCTION__, new_name);
#ifdef CONFIG_SYSFS_DEPRECATED
if ((dev->class) && (dev->parent))
@ -1279,8 +1300,7 @@ static int device_move_class_links(struct device *dev,
class_name);
if (error)
sysfs_remove_link(&dev->kobj, "device");
}
else
} else
error = 0;
out:
kfree(class_name);
@ -1311,16 +1331,13 @@ int device_move(struct device *dev, struct device *new_parent)
return -EINVAL;
new_parent = get_device(new_parent);
new_parent_kobj = get_device_parent (dev, new_parent);
if (IS_ERR(new_parent_kobj)) {
error = PTR_ERR(new_parent_kobj);
put_device(new_parent);
goto out;
}
pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
new_parent ? new_parent->bus_id : "<NULL>");
new_parent_kobj = get_device_parent(dev, new_parent);
pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
__FUNCTION__, new_parent ? new_parent->bus_id : "<NULL>");
error = kobject_move(&dev->kobj, new_parent_kobj);
if (error) {
cleanup_glue_dir(dev, new_parent_kobj);
put_device(new_parent);
goto out;
}
@ -1343,6 +1360,7 @@ int device_move(struct device *dev, struct device *new_parent)
klist_add_tail(&dev->knode_parent,
&old_parent->klist_children);
}
cleanup_glue_dir(dev, new_parent_kobj);
put_device(new_parent);
goto out;
}
@ -1352,5 +1370,23 @@ int device_move(struct device *dev, struct device *new_parent)
put_device(dev);
return error;
}
EXPORT_SYMBOL_GPL(device_move);
/**
* device_shutdown - call ->shutdown() on each device to shutdown.
*/
void device_shutdown(void)
{
struct device *dev, *devn;
list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
kobj.entry) {
if (dev->bus && dev->bus->shutdown) {
dev_dbg(dev, "shutdown\n");
dev->bus->shutdown(dev);
} else if (dev->driver && dev->driver->shutdown) {
dev_dbg(dev, "shutdown\n");
dev->driver->shutdown(dev);
}
}
}

View file

@ -14,7 +14,7 @@
#include "base.h"
struct sysdev_class cpu_sysdev_class = {
set_kset_name("cpu"),
.name = "cpu",
};
EXPORT_SYMBOL(cpu_sysdev_class);

View file

@ -1,18 +1,20 @@
/*
* drivers/base/dd.c - The core device/driver interactions.
* drivers/base/dd.c - The core device/driver interactions.
*
* This file contains the (sometimes tricky) code that controls the
* interactions between devices and drivers, which primarily includes
* driver binding and unbinding.
* This file contains the (sometimes tricky) code that controls the
* interactions between devices and drivers, which primarily includes
* driver binding and unbinding.
*
* All of this code used to exist in drivers/base/bus.c, but was
* relocated to here in the name of compartmentalization (since it wasn't
* strictly code just for the 'struct bus_type'.
* All of this code used to exist in drivers/base/bus.c, but was
* relocated to here in the name of compartmentalization (since it wasn't
* strictly code just for the 'struct bus_type'.
*
* Copyright (c) 2002-5 Patrick Mochel
* Copyright (c) 2002-3 Open Source Development Labs
* Copyright (c) 2002-5 Patrick Mochel
* Copyright (c) 2002-3 Open Source Development Labs
* Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
* Copyright (c) 2007 Novell Inc.
*
* This file is released under the GPLv2
* This file is released under the GPLv2
*/
#include <linux/device.h>
@ -23,8 +25,6 @@
#include "base.h"
#include "power/power.h"
#define to_drv(node) container_of(node, struct device_driver, kobj.entry)
static void driver_bound(struct device *dev)
{
@ -34,27 +34,27 @@ static void driver_bound(struct device *dev)
return;
}
pr_debug("bound device '%s' to driver '%s'\n",
dev->bus_id, dev->driver->name);
pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->bus_id,
__FUNCTION__, dev->driver->name);
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->bus_notifier,
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
BUS_NOTIFY_BOUND_DRIVER, dev);
klist_add_tail(&dev->knode_driver, &dev->driver->klist_devices);
klist_add_tail(&dev->knode_driver, &dev->driver->p->klist_devices);
}
static int driver_sysfs_add(struct device *dev)
{
int ret;
ret = sysfs_create_link(&dev->driver->kobj, &dev->kobj,
ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
kobject_name(&dev->kobj));
if (ret == 0) {
ret = sysfs_create_link(&dev->kobj, &dev->driver->kobj,
ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
"driver");
if (ret)
sysfs_remove_link(&dev->driver->kobj,
sysfs_remove_link(&dev->driver->p->kobj,
kobject_name(&dev->kobj));
}
return ret;
@ -65,24 +65,24 @@ static void driver_sysfs_remove(struct device *dev)
struct device_driver *drv = dev->driver;
if (drv) {
sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
sysfs_remove_link(&dev->kobj, "driver");
}
}
/**
* device_bind_driver - bind a driver to one device.
* @dev: device.
* device_bind_driver - bind a driver to one device.
* @dev: device.
*
* Allow manual attachment of a driver to a device.
* Caller must have already set @dev->driver.
* Allow manual attachment of a driver to a device.
* Caller must have already set @dev->driver.
*
* Note that this does not modify the bus reference count
* nor take the bus's rwsem. Please verify those are accounted
* for before calling this. (It is ok to call with no other effort
* from a driver's probe() method.)
* Note that this does not modify the bus reference count
* nor take the bus's rwsem. Please verify those are accounted
* for before calling this. (It is ok to call with no other effort
* from a driver's probe() method.)
*
* This function must be called with @dev->sem held.
* This function must be called with @dev->sem held.
*/
int device_bind_driver(struct device *dev)
{
@ -93,6 +93,7 @@ int device_bind_driver(struct device *dev)
driver_bound(dev);
return ret;
}
EXPORT_SYMBOL_GPL(device_bind_driver);
static atomic_t probe_count = ATOMIC_INIT(0);
static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
@ -102,8 +103,8 @@ static int really_probe(struct device *dev, struct device_driver *drv)
int ret = 0;
atomic_inc(&probe_count);
pr_debug("%s: Probing driver %s with device %s\n",
drv->bus->name, drv->name, dev->bus_id);
pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
drv->bus->name, __FUNCTION__, drv->name, dev->bus_id);
WARN_ON(!list_empty(&dev->devres_head));
dev->driver = drv;
@ -125,8 +126,8 @@ static int really_probe(struct device *dev, struct device_driver *drv)
driver_bound(dev);
ret = 1;
pr_debug("%s: Bound Device %s to Driver %s\n",
drv->bus->name, dev->bus_id, drv->name);
pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
drv->bus->name, __FUNCTION__, dev->bus_id, drv->name);
goto done;
probe_failed:
@ -183,7 +184,7 @@ int driver_probe_done(void)
* This function must be called with @dev->sem held. When called for a
* USB interface, @dev->parent->sem must be held as well.
*/
int driver_probe_device(struct device_driver * drv, struct device * dev)
int driver_probe_device(struct device_driver *drv, struct device *dev)
{
int ret = 0;
@ -192,8 +193,8 @@ int driver_probe_device(struct device_driver * drv, struct device * dev)
if (drv->bus->match && !drv->bus->match(dev, drv))
goto done;
pr_debug("%s: Matched Device %s with Driver %s\n",
drv->bus->name, dev->bus_id, drv->name);
pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
drv->bus->name, __FUNCTION__, dev->bus_id, drv->name);
ret = really_probe(dev, drv);
@ -201,27 +202,27 @@ int driver_probe_device(struct device_driver * drv, struct device * dev)
return ret;
}
static int __device_attach(struct device_driver * drv, void * data)
static int __device_attach(struct device_driver *drv, void *data)
{
struct device * dev = data;
struct device *dev = data;
return driver_probe_device(drv, dev);
}
/**
* device_attach - try to attach device to a driver.
* @dev: device.
* device_attach - try to attach device to a driver.
* @dev: device.
*
* Walk the list of drivers that the bus has and call
* driver_probe_device() for each pair. If a compatible
* pair is found, break out and return.
* Walk the list of drivers that the bus has and call
* driver_probe_device() for each pair. If a compatible
* pair is found, break out and return.
*
* Returns 1 if the device was bound to a driver;
* 0 if no matching device was found;
* -ENODEV if the device is not registered.
* Returns 1 if the device was bound to a driver;
* 0 if no matching device was found;
* -ENODEV if the device is not registered.
*
* When called for a USB interface, @dev->parent->sem must be held.
* When called for a USB interface, @dev->parent->sem must be held.
*/
int device_attach(struct device * dev)
int device_attach(struct device *dev)
{
int ret = 0;
@ -240,10 +241,11 @@ int device_attach(struct device * dev)
up(&dev->sem);
return ret;
}
EXPORT_SYMBOL_GPL(device_attach);
static int __driver_attach(struct device * dev, void * data)
static int __driver_attach(struct device *dev, void *data)
{
struct device_driver * drv = data;
struct device_driver *drv = data;
/*
* 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)
}
/**
* driver_attach - try to bind driver to devices.
* @drv: driver.
* driver_attach - try to bind driver to devices.
* @drv: driver.
*
* Walk the list of devices that the bus has on it and try to
* match the driver with each one. If driver_probe_device()
* returns 0 and the @dev->driver is set, we've found a
* compatible pair.
* Walk the list of devices that the bus has on it and try to
* match the driver with each one. If driver_probe_device()
* returns 0 and the @dev->driver is set, we've found a
* compatible pair.
*/
int driver_attach(struct device_driver * drv)
int driver_attach(struct device_driver *drv)
{
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
}
EXPORT_SYMBOL_GPL(driver_attach);
/*
* __device_release_driver() must be called with @dev->sem held.
* When called for a USB interface, @dev->parent->sem must be held as well.
* __device_release_driver() must be called with @dev->sem held.
* When called for a USB interface, @dev->parent->sem must be held as well.
*/
static void __device_release_driver(struct device * dev)
static void __device_release_driver(struct device *dev)
{
struct device_driver * drv;
struct device_driver *drv;
drv = get_driver(dev->driver);
drv = dev->driver;
if (drv) {
driver_sysfs_remove(dev);
sysfs_remove_link(&dev->kobj, "driver");
klist_remove(&dev->knode_driver);
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->bus_notifier,
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
BUS_NOTIFY_UNBIND_DRIVER,
dev);
@ -306,18 +308,18 @@ static void __device_release_driver(struct device * dev)
drv->remove(dev);
devres_release_all(dev);
dev->driver = NULL;
put_driver(drv);
klist_remove(&dev->knode_driver);
}
}
/**
* device_release_driver - manually detach device from driver.
* @dev: device.
* device_release_driver - manually detach device from driver.
* @dev: device.
*
* Manually detach device from driver.
* When called for a USB interface, @dev->parent->sem must be held.
* Manually detach device from driver.
* When called for a USB interface, @dev->parent->sem must be held.
*/
void device_release_driver(struct device * dev)
void device_release_driver(struct device *dev)
{
/*
* If anyone calls device_release_driver() recursively from
@ -328,26 +330,26 @@ void device_release_driver(struct device * dev)
__device_release_driver(dev);
up(&dev->sem);
}
EXPORT_SYMBOL_GPL(device_release_driver);
/**
* driver_detach - detach driver from all devices it controls.
* @drv: driver.
*/
void driver_detach(struct device_driver * drv)
void driver_detach(struct device_driver *drv)
{
struct device * dev;
struct device *dev;
for (;;) {
spin_lock(&drv->klist_devices.k_lock);
if (list_empty(&drv->klist_devices.k_list)) {
spin_unlock(&drv->klist_devices.k_lock);
spin_lock(&drv->p->klist_devices.k_lock);
if (list_empty(&drv->p->klist_devices.k_list)) {
spin_unlock(&drv->p->klist_devices.k_lock);
break;
}
dev = list_entry(drv->klist_devices.k_list.prev,
dev = list_entry(drv->p->klist_devices.k_list.prev,
struct device, knode_driver.n_node);
get_device(dev);
spin_unlock(&drv->klist_devices.k_lock);
spin_unlock(&drv->p->klist_devices.k_lock);
if (dev->parent) /* Needed for USB */
down(&dev->parent->sem);
@ -360,9 +362,3 @@ void driver_detach(struct device_driver * drv)
put_device(dev);
}
}
EXPORT_SYMBOL_GPL(device_bind_driver);
EXPORT_SYMBOL_GPL(device_release_driver);
EXPORT_SYMBOL_GPL(device_attach);
EXPORT_SYMBOL_GPL(driver_attach);

View file

@ -3,6 +3,8 @@
*
* Copyright (c) 2002-3 Patrick Mochel
* Copyright (c) 2002-3 Open Source Development Labs
* Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
* Copyright (c) 2007 Novell Inc.
*
* This file is released under the GPLv2
*
@ -15,46 +17,42 @@
#include "base.h"
#define to_dev(node) container_of(node, struct device, driver_list)
#define to_drv(obj) container_of(obj, struct device_driver, kobj)
static struct device * next_device(struct klist_iter * i)
static struct device *next_device(struct klist_iter *i)
{
struct klist_node * n = klist_next(i);
struct klist_node *n = klist_next(i);
return n ? container_of(n, struct device, knode_driver) : NULL;
}
/**
* driver_for_each_device - Iterator for devices bound to a driver.
* @drv: Driver we're iterating.
* @start: Device to begin with
* @data: Data to pass to the callback.
* @fn: Function to call for each device.
* driver_for_each_device - Iterator for devices bound to a driver.
* @drv: Driver we're iterating.
* @start: Device to begin with
* @data: Data to pass to the callback.
* @fn: Function to call for each device.
*
* Iterate over the @drv's list of devices calling @fn for each one.
* Iterate over the @drv's list of devices calling @fn for each one.
*/
int driver_for_each_device(struct device_driver * drv, struct device * start,
void * data, int (*fn)(struct device *, void *))
int driver_for_each_device(struct device_driver *drv, struct device *start,
void *data, int (*fn)(struct device *, void *))
{
struct klist_iter i;
struct device * dev;
struct device *dev;
int error = 0;
if (!drv)
return -EINVAL;
klist_iter_init_node(&drv->klist_devices, &i,
klist_iter_init_node(&drv->p->klist_devices, &i,
start ? &start->knode_driver : NULL);
while ((dev = next_device(&i)) && !error)
error = fn(dev, data);
klist_iter_exit(&i);
return error;
}
EXPORT_SYMBOL_GPL(driver_for_each_device);
/**
* driver_find_device - device iterator for locating a particular device.
* @drv: The device's driver
@ -70,9 +68,9 @@ EXPORT_SYMBOL_GPL(driver_for_each_device);
* if it does. If the callback returns non-zero, this function will
* return to the caller and not iterate over any more devices.
*/
struct device * driver_find_device(struct device_driver *drv,
struct device * start, void * data,
int (*match)(struct device *, void *))
struct device *driver_find_device(struct device_driver *drv,
struct device *start, void *data,
int (*match)(struct device *dev, void *data))
{
struct klist_iter i;
struct device *dev;
@ -80,7 +78,7 @@ struct device * driver_find_device(struct device_driver *drv,
if (!drv)
return NULL;
klist_iter_init_node(&drv->klist_devices, &i,
klist_iter_init_node(&drv->p->klist_devices, &i,
(start ? &start->knode_driver : NULL));
while ((dev = next_device(&i)))
if (match(dev, data) && get_device(dev))
@ -91,111 +89,179 @@ struct device * driver_find_device(struct device_driver *drv,
EXPORT_SYMBOL_GPL(driver_find_device);
/**
* driver_create_file - create sysfs file for driver.
* @drv: driver.
* @attr: driver attribute descriptor.
* driver_create_file - create sysfs file for driver.
* @drv: driver.
* @attr: driver attribute descriptor.
*/
int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
int driver_create_file(struct device_driver *drv,
struct driver_attribute *attr)
{
int error;
if (get_driver(drv)) {
error = sysfs_create_file(&drv->kobj, &attr->attr);
error = sysfs_create_file(&drv->p->kobj, &attr->attr);
put_driver(drv);
} else
error = -EINVAL;
return error;
}
EXPORT_SYMBOL_GPL(driver_create_file);
/**
* driver_remove_file - remove sysfs file for driver.
* @drv: driver.
* @attr: driver attribute descriptor.
* driver_remove_file - remove sysfs file for driver.
* @drv: driver.
* @attr: driver attribute descriptor.
*/
void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
void driver_remove_file(struct device_driver *drv,
struct driver_attribute *attr)
{
if (get_driver(drv)) {
sysfs_remove_file(&drv->kobj, &attr->attr);
sysfs_remove_file(&drv->p->kobj, &attr->attr);
put_driver(drv);
}
}
EXPORT_SYMBOL_GPL(driver_remove_file);
/**
* get_driver - increment driver reference count.
* @drv: driver.
*/
struct device_driver * get_driver(struct device_driver * drv)
{
return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
}
/**
* put_driver - decrement driver's refcount.
* @drv: driver.
*/
void put_driver(struct device_driver * drv)
{
kobject_put(&drv->kobj);
}
/**
* driver_register - register driver with bus
* @drv: driver to register
* driver_add_kobj - add a kobject below the specified driver
*
* We pass off most of the work to the bus_add_driver() call,
* since most of the things we have to do deal with the bus
* structures.
* You really don't want to do this, this is only here due to one looney
* iseries driver, go poke those developers if you are annoyed about
* this...
*/
int driver_register(struct device_driver * drv)
int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
const char *fmt, ...)
{
va_list args;
char *name;
va_start(args, fmt);
name = kvasprintf(GFP_KERNEL, fmt, args);
va_end(args);
if (!name)
return -ENOMEM;
return kobject_add(kobj, &drv->p->kobj, "%s", name);
}
EXPORT_SYMBOL_GPL(driver_add_kobj);
/**
* get_driver - increment driver reference count.
* @drv: driver.
*/
struct device_driver *get_driver(struct device_driver *drv)
{
if (drv) {
struct driver_private *priv;
struct kobject *kobj;
kobj = kobject_get(&drv->p->kobj);
priv = to_driver(kobj);
return priv->driver;
}
return NULL;
}
EXPORT_SYMBOL_GPL(get_driver);
/**
* put_driver - decrement driver's refcount.
* @drv: driver.
*/
void put_driver(struct device_driver *drv)
{
kobject_put(&drv->p->kobj);
}
EXPORT_SYMBOL_GPL(put_driver);
static int driver_add_groups(struct device_driver *drv,
struct attribute_group **groups)
{
int error = 0;
int i;
if (groups) {
for (i = 0; groups[i]; i++) {
error = sysfs_create_group(&drv->p->kobj, groups[i]);
if (error) {
while (--i >= 0)
sysfs_remove_group(&drv->p->kobj,
groups[i]);
break;
}
}
}
return error;
}
static void driver_remove_groups(struct device_driver *drv,
struct attribute_group **groups)
{
int i;
if (groups)
for (i = 0; groups[i]; i++)
sysfs_remove_group(&drv->p->kobj, groups[i]);
}
/**
* driver_register - register driver with bus
* @drv: driver to register
*
* We pass off most of the work to the bus_add_driver() call,
* since most of the things we have to do deal with the bus
* structures.
*/
int driver_register(struct device_driver *drv)
{
int ret;
if ((drv->bus->probe && drv->probe) ||
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown)) {
printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
}
klist_init(&drv->klist_devices, NULL, NULL);
return bus_add_driver(drv);
(drv->bus->shutdown && drv->shutdown))
printk(KERN_WARNING "Driver '%s' needs updating - please use "
"bus_type methods\n", drv->name);
ret = bus_add_driver(drv);
if (ret)
return ret;
ret = driver_add_groups(drv, drv->groups);
if (ret)
bus_remove_driver(drv);
return ret;
}
EXPORT_SYMBOL_GPL(driver_register);
/**
* driver_unregister - remove driver from system.
* @drv: driver.
* driver_unregister - remove driver from system.
* @drv: driver.
*
* Again, we pass off most of the work to the bus-level call.
* Again, we pass off most of the work to the bus-level call.
*/
void driver_unregister(struct device_driver * drv)
void driver_unregister(struct device_driver *drv)
{
driver_remove_groups(drv, drv->groups);
bus_remove_driver(drv);
}
EXPORT_SYMBOL_GPL(driver_unregister);
/**
* driver_find - locate driver on a bus by its name.
* @name: name of the driver.
* @bus: bus to scan for the driver.
* driver_find - locate driver on a bus by its name.
* @name: name of the driver.
* @bus: bus to scan for the driver.
*
* Call kset_find_obj() to iterate over list of drivers on
* a bus to find driver by name. Return driver if found.
* Call kset_find_obj() to iterate over list of drivers on
* a bus to find driver by name. Return driver if found.
*
* Note that kset_find_obj increments driver's reference count.
* Note that kset_find_obj increments driver's reference count.
*/
struct device_driver *driver_find(const char *name, struct bus_type *bus)
{
struct kobject *k = kset_find_obj(&bus->drivers, name);
if (k)
return to_drv(k);
struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
struct driver_private *priv;
if (k) {
priv = to_driver(k);
return priv->driver;
}
return NULL;
}
EXPORT_SYMBOL_GPL(driver_register);
EXPORT_SYMBOL_GPL(driver_unregister);
EXPORT_SYMBOL_GPL(get_driver);
EXPORT_SYMBOL_GPL(put_driver);
EXPORT_SYMBOL_GPL(driver_find);
EXPORT_SYMBOL_GPL(driver_create_file);
EXPORT_SYMBOL_GPL(driver_remove_file);

View file

@ -3,11 +3,11 @@
*
* Copyright (c) 2002-3 Patrick Mochel
* Copyright (c) 2002-3 Open Source Development Labs
* Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
* Copyright (c) 2007 Novell Inc.
*
* This file is released under the GPLv2
*
*/
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/init.h>
@ -15,23 +15,13 @@
#include "base.h"
static decl_subsys(firmware, NULL, NULL);
int firmware_register(struct kset *s)
{
kobj_set_kset_s(s, firmware_subsys);
return subsystem_register(s);
}
void firmware_unregister(struct kset *s)
{
subsystem_unregister(s);
}
struct kobject *firmware_kobj;
EXPORT_SYMBOL_GPL(firmware_kobj);
int __init firmware_init(void)
{
return subsystem_register(&firmware_subsys);
firmware_kobj = kobject_create_and_add("firmware", NULL);
if (!firmware_kobj)
return -ENOMEM;
return 0;
}
EXPORT_SYMBOL_GPL(firmware_register);
EXPORT_SYMBOL_GPL(firmware_unregister);

View file

@ -2,19 +2,23 @@
* hypervisor.c - /sys/hypervisor subsystem.
*
* Copyright (C) IBM Corp. 2006
* Copyright (C) 2007 Greg Kroah-Hartman <gregkh@suse.de>
* Copyright (C) 2007 Novell Inc.
*
* This file is released under the GPLv2
*/
#include <linux/kobject.h>
#include <linux/device.h>
#include "base.h"
decl_subsys(hypervisor, NULL, NULL);
EXPORT_SYMBOL_GPL(hypervisor_subsys);
struct kobject *hypervisor_kobj;
EXPORT_SYMBOL_GPL(hypervisor_kobj);
int __init hypervisor_init(void)
{
return subsystem_register(&hypervisor_subsys);
hypervisor_kobj = kobject_create_and_add("hypervisor", NULL);
if (!hypervisor_kobj)
return -ENOMEM;
return 0;
}

View file

@ -1,10 +1,8 @@
/*
*
* Copyright (c) 2002-3 Patrick Mochel
* Copyright (c) 2002-3 Open Source Development Labs
*
* This file is released under the GPLv2
*
*/
#include <linux/device.h>
@ -14,12 +12,11 @@
#include "base.h"
/**
* driver_init - initialize driver model.
* driver_init - initialize driver model.
*
* Call the driver model init functions to initialize their
* subsystems. Called early from init/main.c.
* Call the driver model init functions to initialize their
* subsystems. Called early from init/main.c.
*/
void __init driver_init(void)
{
/* These are the core pieces */
@ -36,5 +33,4 @@ void __init driver_init(void)
system_bus_init();
cpu_dev_init();
memory_dev_init();
attribute_container_init();
}

View file

@ -26,7 +26,7 @@
#define MEMORY_CLASS_NAME "memory"
static struct sysdev_class memory_sysdev_class = {
set_kset_name(MEMORY_CLASS_NAME),
.name = MEMORY_CLASS_NAME,
};
static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)

94
drivers/base/module.c Normal file
View file

@ -0,0 +1,94 @@
/*
* module.c - module sysfs fun for drivers
*
* This file is released under the GPLv2
*
*/
#include <linux/device.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/string.h>
#include "base.h"
static char *make_driver_name(struct device_driver *drv)
{
char *driver_name;
driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
GFP_KERNEL);
if (!driver_name)
return NULL;
sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
return driver_name;
}
static void module_create_drivers_dir(struct module_kobject *mk)
{
if (!mk || mk->drivers_dir)
return;
mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj);
}
void module_add_driver(struct module *mod, struct device_driver *drv)
{
char *driver_name;
int no_warn;
struct module_kobject *mk = NULL;
if (!drv)
return;
if (mod)
mk = &mod->mkobj;
else if (drv->mod_name) {
struct kobject *mkobj;
/* Lookup built-in module entry in /sys/modules */
mkobj = kset_find_obj(module_kset, drv->mod_name);
if (mkobj) {
mk = container_of(mkobj, struct module_kobject, kobj);
/* remember our module structure */
drv->p->mkobj = mk;
/* kset_find_obj took a reference */
kobject_put(mkobj);
}
}
if (!mk)
return;
/* Don't check return codes; these calls are idempotent */
no_warn = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module");
driver_name = make_driver_name(drv);
if (driver_name) {
module_create_drivers_dir(mk);
no_warn = sysfs_create_link(mk->drivers_dir, &drv->p->kobj,
driver_name);
kfree(driver_name);
}
}
void module_remove_driver(struct device_driver *drv)
{
struct module_kobject *mk = NULL;
char *driver_name;
if (!drv)
return;
sysfs_remove_link(&drv->p->kobj, "module");
if (drv->owner)
mk = &drv->owner->mkobj;
else if (drv->p->mkobj)
mk = drv->p->mkobj;
if (mk && mk->drivers_dir) {
driver_name = make_driver_name(drv);
if (driver_name) {
sysfs_remove_link(mk->drivers_dir, driver_name);
kfree(driver_name);
}
}
}

View file

@ -15,7 +15,7 @@
#include <linux/device.h>
static struct sysdev_class node_class = {
set_kset_name("node"),
.name = "node",
};

View file

@ -20,7 +20,8 @@
#include "base.h"
#define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
driver))
struct device platform_bus = {
.bus_id = "platform",
@ -28,14 +29,13 @@ struct device platform_bus = {
EXPORT_SYMBOL_GPL(platform_bus);
/**
* platform_get_resource - get a resource for a device
* @dev: platform device
* @type: resource type
* @num: resource index
* platform_get_resource - get a resource for a device
* @dev: platform device
* @type: resource type
* @num: resource index
*/
struct resource *
platform_get_resource(struct platform_device *dev, unsigned int type,
unsigned int num)
struct resource *platform_get_resource(struct platform_device *dev,
unsigned int type, unsigned int num)
{
int i;
@ -43,8 +43,7 @@ platform_get_resource(struct platform_device *dev, unsigned int type,
struct resource *r = &dev->resource[i];
if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
IORESOURCE_IRQ|IORESOURCE_DMA))
== type)
IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
if (num-- == 0)
return r;
}
@ -53,9 +52,9 @@ platform_get_resource(struct platform_device *dev, unsigned int type,
EXPORT_SYMBOL_GPL(platform_get_resource);
/**
* platform_get_irq - get an IRQ for a device
* @dev: platform device
* @num: IRQ number index
* platform_get_irq - get an IRQ for a device
* @dev: platform device
* @num: IRQ number index
*/
int platform_get_irq(struct platform_device *dev, unsigned int num)
{
@ -66,14 +65,13 @@ int platform_get_irq(struct platform_device *dev, unsigned int num)
EXPORT_SYMBOL_GPL(platform_get_irq);
/**
* platform_get_resource_byname - get a resource for a device by name
* @dev: platform device
* @type: resource type
* @name: resource name
* platform_get_resource_byname - get a resource for a device by name
* @dev: platform device
* @type: resource type
* @name: resource name
*/
struct resource *
platform_get_resource_byname(struct platform_device *dev, unsigned int type,
char *name)
struct resource *platform_get_resource_byname(struct platform_device *dev,
unsigned int type, char *name)
{
int i;
@ -90,22 +88,23 @@ platform_get_resource_byname(struct platform_device *dev, unsigned int type,
EXPORT_SYMBOL_GPL(platform_get_resource_byname);
/**
* platform_get_irq - get an IRQ for a device
* @dev: platform device
* @name: IRQ name
* platform_get_irq - get an IRQ for a device
* @dev: platform device
* @name: IRQ name
*/
int platform_get_irq_byname(struct platform_device *dev, char *name)
{
struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
name);
return r ? r->start : -ENXIO;
}
EXPORT_SYMBOL_GPL(platform_get_irq_byname);
/**
* platform_add_devices - add a numbers of platform devices
* @devs: array of platform devices to add
* @num: number of platform devices in array
* platform_add_devices - add a numbers of platform devices
* @devs: array of platform devices to add
* @num: number of platform devices in array
*/
int platform_add_devices(struct platform_device **devs, int num)
{
@ -130,12 +129,11 @@ struct platform_object {
};
/**
* platform_device_put
* @pdev: platform device to free
* platform_device_put
* @pdev: platform device to free
*
* Free all memory associated with a platform device. This function
* must _only_ be externally called in error cases. All other usage
* is a bug.
* Free all memory associated with a platform device. This function must
* _only_ be externally called in error cases. All other usage is a bug.
*/
void platform_device_put(struct platform_device *pdev)
{
@ -146,7 +144,8 @@ EXPORT_SYMBOL_GPL(platform_device_put);
static void platform_device_release(struct device *dev)
{
struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
struct platform_object *pa = container_of(dev, struct platform_object,
pdev.dev);
kfree(pa->pdev.dev.platform_data);
kfree(pa->pdev.resource);
@ -154,12 +153,12 @@ static void platform_device_release(struct device *dev)
}
/**
* platform_device_alloc
* @name: base name of the device we're adding
* @id: instance id
* platform_device_alloc
* @name: base name of the device we're adding
* @id: instance id
*
* Create a platform device object which can have other objects attached
* to it, and which will have attached objects freed when it is released.
* Create a platform device object which can have other objects attached
* to it, and which will have attached objects freed when it is released.
*/
struct platform_device *platform_device_alloc(const char *name, int id)
{
@ -179,16 +178,17 @@ struct platform_device *platform_device_alloc(const char *name, int id)
EXPORT_SYMBOL_GPL(platform_device_alloc);
/**
* platform_device_add_resources
* @pdev: platform device allocated by platform_device_alloc to add resources to
* @res: set of resources that needs to be allocated for the device
* @num: number of resources
* platform_device_add_resources
* @pdev: platform device allocated by platform_device_alloc to add resources to
* @res: set of resources that needs to be allocated for the device
* @num: number of resources
*
* Add a copy of the resources to the platform device. The memory
* associated with the resources will be freed when the platform
* device is released.
* Add a copy of the resources to the platform device. The memory
* associated with the resources will be freed when the platform device is
* released.
*/
int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
int platform_device_add_resources(struct platform_device *pdev,
struct resource *res, unsigned int num)
{
struct resource *r;
@ -203,16 +203,17 @@ int platform_device_add_resources(struct platform_device *pdev, struct resource
EXPORT_SYMBOL_GPL(platform_device_add_resources);
/**
* platform_device_add_data
* @pdev: platform device allocated by platform_device_alloc to add resources to
* @data: platform specific data for this platform device
* @size: size of platform specific data
* platform_device_add_data
* @pdev: platform device allocated by platform_device_alloc to add resources to
* @data: platform specific data for this platform device
* @size: size of platform specific data
*
* Add a copy of platform specific data to the platform device's platform_data
* pointer. The memory associated with the platform data will be freed
* when the platform device is released.
* Add a copy of platform specific data to the platform device's
* platform_data pointer. The memory associated with the platform data
* will be freed when the platform device is released.
*/
int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size)
int platform_device_add_data(struct platform_device *pdev, const void *data,
size_t size)
{
void *d;
@ -226,11 +227,11 @@ int platform_device_add_data(struct platform_device *pdev, const void *data, siz
EXPORT_SYMBOL_GPL(platform_device_add_data);
/**
* platform_device_add - add a platform device to device hierarchy
* @pdev: platform device we're adding
* platform_device_add - add a platform device to device hierarchy
* @pdev: platform device we're adding
*
* This is part 2 of platform_device_register(), though may be called
* separately _iff_ pdev was allocated by platform_device_alloc().
* This is part 2 of platform_device_register(), though may be called
* separately _iff_ pdev was allocated by platform_device_alloc().
*/
int platform_device_add(struct platform_device *pdev)
{
@ -289,13 +290,12 @@ int platform_device_add(struct platform_device *pdev)
EXPORT_SYMBOL_GPL(platform_device_add);
/**
* platform_device_del - remove a platform-level device
* @pdev: platform device we're removing
* platform_device_del - remove a platform-level device
* @pdev: platform device we're removing
*
* Note that this function will also release all memory- and port-based
* resources owned by the device (@dev->resource). This function
* must _only_ be externally called in error cases. All other usage
* is a bug.
* Note that this function will also release all memory- and port-based
* resources owned by the device (@dev->resource). This function must
* _only_ be externally called in error cases. All other usage is a bug.
*/
void platform_device_del(struct platform_device *pdev)
{
@ -314,11 +314,10 @@ void platform_device_del(struct platform_device *pdev)
EXPORT_SYMBOL_GPL(platform_device_del);
/**
* platform_device_register - add a platform-level device
* @pdev: platform device we're adding
*
* platform_device_register - add a platform-level device
* @pdev: platform device we're adding
*/
int platform_device_register(struct platform_device * pdev)
int platform_device_register(struct platform_device *pdev)
{
device_initialize(&pdev->dev);
return platform_device_add(pdev);
@ -326,14 +325,14 @@ int platform_device_register(struct platform_device * pdev)
EXPORT_SYMBOL_GPL(platform_device_register);
/**
* platform_device_unregister - unregister a platform-level device
* @pdev: platform device we're unregistering
* platform_device_unregister - unregister a platform-level device
* @pdev: platform device we're unregistering
*
* Unregistration is done in 2 steps. First we release all resources
* and remove it from the subsystem, then we drop reference count by
* calling platform_device_put().
* Unregistration is done in 2 steps. First we release all resources
* and remove it from the subsystem, then we drop reference count by
* calling platform_device_put().
*/
void platform_device_unregister(struct platform_device * pdev)
void platform_device_unregister(struct platform_device *pdev)
{
platform_device_del(pdev);
platform_device_put(pdev);
@ -341,27 +340,29 @@ void platform_device_unregister(struct platform_device * pdev)
EXPORT_SYMBOL_GPL(platform_device_unregister);
/**
* platform_device_register_simple
* @name: base name of the device we're adding
* @id: instance id
* @res: set of resources that needs to be allocated for the device
* @num: number of resources
* platform_device_register_simple
* @name: base name of the device we're adding
* @id: instance id
* @res: set of resources that needs to be allocated for the device
* @num: number of resources
*
* This function creates a simple platform device that requires minimal
* resource and memory management. Canned release function freeing
* memory allocated for the device allows drivers using such devices
* to be unloaded without waiting for the last reference to the device
* to be dropped.
* This function creates a simple platform device that requires minimal
* resource and memory management. Canned release function freeing memory
* allocated for the device allows drivers using such devices to be
* unloaded without waiting for the last reference to the device to be
* dropped.
*
* This interface is primarily intended for use with legacy drivers
* which probe hardware directly. Because such drivers create sysfs
* device nodes themselves, rather than letting system infrastructure
* handle such device enumeration tasks, they don't fully conform to
* the Linux driver model. In particular, when such drivers are built
* as modules, they can't be "hotplugged".
* This interface is primarily intended for use with legacy drivers which
* probe hardware directly. Because such drivers create sysfs device nodes
* themselves, rather than letting system infrastructure handle such device
* enumeration tasks, they don't fully conform to the Linux driver model.
* In particular, when such drivers are built as modules, they can't be
* "hotplugged".
*/
struct platform_device *platform_device_register_simple(char *name, int id,
struct resource *res, unsigned int num)
struct platform_device *platform_device_register_simple(const char *name,
int id,
struct resource *res,
unsigned int num)
{
struct platform_device *pdev;
int retval;
@ -436,8 +437,8 @@ static int platform_drv_resume(struct device *_dev)
}
/**
* platform_driver_register
* @drv: platform driver structure
* platform_driver_register
* @drv: platform driver structure
*/
int platform_driver_register(struct platform_driver *drv)
{
@ -457,8 +458,8 @@ int platform_driver_register(struct platform_driver *drv)
EXPORT_SYMBOL_GPL(platform_driver_register);
/**
* platform_driver_unregister
* @drv: platform driver structure
* platform_driver_unregister
* @drv: platform driver structure
*/
void platform_driver_unregister(struct platform_driver *drv)
{
@ -497,12 +498,12 @@ int __init_or_module platform_driver_probe(struct platform_driver *drv,
* if the probe was successful, and make sure any forced probes of
* new devices fail.
*/
spin_lock(&platform_bus_type.klist_drivers.k_lock);
spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
drv->probe = NULL;
if (code == 0 && list_empty(&drv->driver.klist_devices.k_list))
if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
retval = -ENODEV;
drv->driver.probe = platform_drv_probe_fail;
spin_unlock(&platform_bus_type.klist_drivers.k_lock);
spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
if (code != retval)
platform_driver_unregister(drv);
@ -516,8 +517,8 @@ EXPORT_SYMBOL_GPL(platform_driver_probe);
* (b) sysfs attribute lets new-style coldplug recover from hotplug events
* mishandled before system is fully running: "modprobe $(cat modalias)"
*/
static ssize_t
modalias_show(struct device *dev, struct device_attribute *a, char *buf)
static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
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)
return 0;
}
/**
* platform_match - bind platform device to platform driver.
* @dev: device.
* @drv: driver.
* platform_match - bind platform device to platform driver.
* @dev: device.
* @drv: driver.
*
* Platform device IDs are assumed to be encoded like this:
* "<name><instance>", where <name> is a short description of the
* type of device, like "pci" or "floppy", and <instance> is the
* enumerated instance of the device, like '0' or '42'.
* Driver IDs are simply "<name>".
* So, extract the <name> from the platform_device structure,
* and compare it against the name of the driver. Return whether
* they match or not.
* Platform device IDs are assumed to be encoded like this:
* "<name><instance>", where <name> is a short description of the type of
* device, like "pci" or "floppy", and <instance> is the enumerated
* instance of the device, like '0' or '42'. Driver IDs are simply
* "<name>". So, extract the <name> from the platform_device structure,
* and compare it against the name of the driver. Return whether they match
* or not.
*/
static int platform_match(struct device * dev, struct device_driver * drv)
static int platform_match(struct device *dev, struct device_driver *drv)
{
struct platform_device *pdev = container_of(dev, struct platform_device, dev);
struct platform_device *pdev;
pdev = container_of(dev, struct platform_device, dev);
return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
}
@ -574,9 +573,10 @@ static int platform_suspend(struct device *dev, pm_message_t mesg)
static int platform_suspend_late(struct device *dev, pm_message_t mesg)
{
struct platform_driver *drv = to_platform_driver(dev->driver);
struct platform_device *pdev = container_of(dev, struct platform_device, dev);
struct platform_device *pdev;
int ret = 0;
pdev = container_of(dev, struct platform_device, dev);
if (dev->driver && drv->suspend_late)
ret = drv->suspend_late(pdev, mesg);
@ -586,16 +586,17 @@ static int platform_suspend_late(struct device *dev, pm_message_t mesg)
static int platform_resume_early(struct device *dev)
{
struct platform_driver *drv = to_platform_driver(dev->driver);
struct platform_device *pdev = container_of(dev, struct platform_device, dev);
struct platform_device *pdev;
int ret = 0;
pdev = container_of(dev, struct platform_device, dev);
if (dev->driver && drv->resume_early)
ret = drv->resume_early(pdev);
return ret;
}
static int platform_resume(struct device * dev)
static int platform_resume(struct device *dev)
{
int ret = 0;

View file

@ -1,4 +1,3 @@
obj-y := shutdown.o
obj-$(CONFIG_PM) += sysfs.o
obj-$(CONFIG_PM_SLEEP) += main.o
obj-$(CONFIG_PM_TRACE) += trace.o

View file

@ -24,20 +24,45 @@
#include <linux/mutex.h>
#include <linux/pm.h>
#include <linux/resume-trace.h>
#include <linux/rwsem.h>
#include "../base.h"
#include "power.h"
/*
* The entries in the dpm_active list are in a depth first order, simply
* because children are guaranteed to be discovered after parents, and
* are inserted at the back of the list on discovery.
*
* All the other lists are kept in the same order, for consistency.
* However the lists aren't always traversed in the same order.
* Semaphores must be acquired from the top (i.e., front) down
* and released in the opposite order. Devices must be suspended
* from the bottom (i.e., end) up and resumed in the opposite order.
* That way no parent will be suspended while it still has an active
* child.
*
* Since device_pm_add() may be called with a device semaphore held,
* we must never try to acquire a device semaphore while holding
* dpm_list_mutex.
*/
LIST_HEAD(dpm_active);
static LIST_HEAD(dpm_locked);
static LIST_HEAD(dpm_off);
static LIST_HEAD(dpm_off_irq);
static LIST_HEAD(dpm_destroy);
static DEFINE_MUTEX(dpm_mtx);
static DEFINE_MUTEX(dpm_list_mtx);
static DECLARE_RWSEM(pm_sleep_rwsem);
int (*platform_enable_wakeup)(struct device *dev, int is_on);
/**
* device_pm_add - add a device to the list of active devices
* @dev: Device to be added to the list
*/
void device_pm_add(struct device *dev)
{
pr_debug("PM: Adding info for %s:%s\n",
@ -48,8 +73,36 @@ void device_pm_add(struct device *dev)
mutex_unlock(&dpm_list_mtx);
}
/**
* device_pm_remove - remove a device from the list of active devices
* @dev: Device to be removed from the list
*
* This function also removes the device's PM-related sysfs attributes.
*/
void device_pm_remove(struct device *dev)
{
/*
* If this function is called during a suspend, it will be blocked,
* because we're holding the device's semaphore at that time, which may
* lead to a deadlock. In that case we want to print a warning.
* However, it may also be called by unregister_dropped_devices() with
* the device's semaphore released, in which case the warning should
* not be printed.
*/
if (down_trylock(&dev->sem)) {
if (down_read_trylock(&pm_sleep_rwsem)) {
/* No suspend in progress, wait on dev->sem */
down(&dev->sem);
up_read(&pm_sleep_rwsem);
} else {
/* Suspend in progress, we may deadlock */
dev_warn(dev, "Suspicious %s during suspend\n",
__FUNCTION__);
dump_stack();
/* The user has been warned ... */
down(&dev->sem);
}
}
pr_debug("PM: Removing info for %s:%s\n",
dev->bus ? dev->bus->name : "No Bus",
kobject_name(&dev->kobj));
@ -57,25 +110,124 @@ void device_pm_remove(struct device *dev)
dpm_sysfs_remove(dev);
list_del_init(&dev->power.entry);
mutex_unlock(&dpm_list_mtx);
up(&dev->sem);
}
/**
* device_pm_schedule_removal - schedule the removal of a suspended device
* @dev: Device to destroy
*
* Moves the device to the dpm_destroy list for further processing by
* unregister_dropped_devices().
*/
void device_pm_schedule_removal(struct device *dev)
{
pr_debug("PM: Preparing for removal: %s:%s\n",
dev->bus ? dev->bus->name : "No Bus",
kobject_name(&dev->kobj));
mutex_lock(&dpm_list_mtx);
list_move_tail(&dev->power.entry, &dpm_destroy);
mutex_unlock(&dpm_list_mtx);
}
/**
* pm_sleep_lock - mutual exclusion for registration and suspend
*
* Returns 0 if no suspend is underway and device registration
* may proceed, otherwise -EBUSY.
*/
int pm_sleep_lock(void)
{
if (down_read_trylock(&pm_sleep_rwsem))
return 0;
return -EBUSY;
}
/**
* pm_sleep_unlock - mutual exclusion for registration and suspend
*
* This routine undoes the effect of device_pm_add_lock
* when a device's registration is complete.
*/
void pm_sleep_unlock(void)
{
up_read(&pm_sleep_rwsem);
}
/*------------------------- Resume routines -------------------------*/
/**
* resume_device - Restore state for one device.
* resume_device_early - Power on one device (early resume).
* @dev: Device.
*
* Must be called with interrupts disabled.
*/
static int resume_device(struct device * dev)
static int resume_device_early(struct device *dev)
{
int error = 0;
TRACE_DEVICE(dev);
TRACE_RESUME(0);
down(&dev->sem);
if (dev->bus && dev->bus->resume_early) {
dev_dbg(dev, "EARLY resume\n");
error = dev->bus->resume_early(dev);
}
TRACE_RESUME(error);
return error;
}
/**
* dpm_power_up - Power on all regular (non-sysdev) devices.
*
* Walk the dpm_off_irq list and power each device up. This
* is used for devices that required they be powered down with
* interrupts disabled. As devices are powered on, they are moved
* to the dpm_off list.
*
* Must be called with interrupts disabled and only one CPU running.
*/
static void dpm_power_up(void)
{
while (!list_empty(&dpm_off_irq)) {
struct list_head *entry = dpm_off_irq.next;
struct device *dev = to_device(entry);
list_move_tail(entry, &dpm_off);
resume_device_early(dev);
}
}
/**
* device_power_up - Turn on all devices that need special attention.
*
* Power on system devices, then devices that required we shut them down
* with interrupts disabled.
*
* Must be called with interrupts disabled.
*/
void device_power_up(void)
{
sysdev_resume();
dpm_power_up();
}
EXPORT_SYMBOL_GPL(device_power_up);
/**
* resume_device - Restore state for one device.
* @dev: Device.
*
*/
static int resume_device(struct device *dev)
{
int error = 0;
TRACE_DEVICE(dev);
TRACE_RESUME(0);
if (dev->bus && dev->bus->resume) {
dev_dbg(dev,"resuming\n");
@ -92,126 +244,94 @@ static int resume_device(struct device * dev)
error = dev->class->resume(dev);
}
up(&dev->sem);
TRACE_RESUME(error);
return error;
}
static int resume_device_early(struct device * dev)
{
int error = 0;
TRACE_DEVICE(dev);
TRACE_RESUME(0);
if (dev->bus && dev->bus->resume_early) {
dev_dbg(dev,"EARLY resume\n");
error = dev->bus->resume_early(dev);
}
TRACE_RESUME(error);
return error;
}
/*
* Resume the devices that have either not gone through
* the late suspend, or that did go through it but also
* went through the early resume
/**
* dpm_resume - Resume every device.
*
* Resume the devices that have either not gone through
* the late suspend, or that did go through it but also
* went through the early resume.
*
* Take devices from the dpm_off_list, resume them,
* and put them on the dpm_locked list.
*/
static void dpm_resume(void)
{
mutex_lock(&dpm_list_mtx);
while(!list_empty(&dpm_off)) {
struct list_head * entry = dpm_off.next;
struct device * dev = to_device(entry);
get_device(dev);
list_move_tail(entry, &dpm_active);
struct list_head *entry = dpm_off.next;
struct device *dev = to_device(entry);
list_move_tail(entry, &dpm_locked);
mutex_unlock(&dpm_list_mtx);
resume_device(dev);
mutex_lock(&dpm_list_mtx);
put_device(dev);
}
mutex_unlock(&dpm_list_mtx);
}
/**
* unlock_all_devices - Release each device's semaphore
*
* Go through the dpm_off list. Put each device on the dpm_active
* list and unlock it.
*/
static void unlock_all_devices(void)
{
mutex_lock(&dpm_list_mtx);
while (!list_empty(&dpm_locked)) {
struct list_head *entry = dpm_locked.prev;
struct device *dev = to_device(entry);
list_move(entry, &dpm_active);
up(&dev->sem);
}
mutex_unlock(&dpm_list_mtx);
}
/**
* unregister_dropped_devices - Unregister devices scheduled for removal
*
* Unregister all devices on the dpm_destroy list.
*/
static void unregister_dropped_devices(void)
{
mutex_lock(&dpm_list_mtx);
while (!list_empty(&dpm_destroy)) {
struct list_head *entry = dpm_destroy.next;
struct device *dev = to_device(entry);
up(&dev->sem);
mutex_unlock(&dpm_list_mtx);
/* This also removes the device from the list */
device_unregister(dev);
mutex_lock(&dpm_list_mtx);
}
mutex_unlock(&dpm_list_mtx);
}
/**
* device_resume - Restore state of each device in system.
*
* Walk the dpm_off list, remove each entry, resume the device,
* then add it to the dpm_active list.
* Resume all the devices, unlock them all, and allow new
* devices to be registered once again.
*/
void device_resume(void)
{
might_sleep();
mutex_lock(&dpm_mtx);
dpm_resume();
mutex_unlock(&dpm_mtx);
unlock_all_devices();
unregister_dropped_devices();
up_write(&pm_sleep_rwsem);
}
EXPORT_SYMBOL_GPL(device_resume);
/**
* dpm_power_up - Power on some devices.
*
* Walk the dpm_off_irq list and power each device up. This
* is used for devices that required they be powered down with
* interrupts disabled. As devices are powered on, they are moved
* to the dpm_active list.
*
* Interrupts must be disabled when calling this.
*/
static void dpm_power_up(void)
{
while(!list_empty(&dpm_off_irq)) {
struct list_head * entry = dpm_off_irq.next;
struct device * dev = to_device(entry);
list_move_tail(entry, &dpm_off);
resume_device_early(dev);
}
}
/**
* device_power_up - Turn on all devices that need special attention.
*
* Power on system devices then devices that required we shut them down
* with interrupts disabled.
* Called with interrupts disabled.
*/
void device_power_up(void)
{
sysdev_resume();
dpm_power_up();
}
EXPORT_SYMBOL_GPL(device_power_up);
/*------------------------- Suspend routines -------------------------*/
/*
* The entries in the dpm_active list are in a depth first order, simply
* because children are guaranteed to be discovered after parents, and
* are inserted at the back of the list on discovery.
*
* All list on the suspend path are done in reverse order, so we operate
* on the leaves of the device tree (or forests, depending on how you want
* to look at it ;) first. As nodes are removed from the back of the list,
* they are inserted into the front of their destintation lists.
*
* Things are the reverse on the resume path - iterations are done in
* forward order, and nodes are inserted at the back of their destination
* lists. This way, the ancestors will be accessed before their descendents.
*/
static inline char *suspend_verb(u32 event)
{
switch (event) {
@ -222,7 +342,6 @@ static inline char *suspend_verb(u32 event)
}
}
static void
suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
{
@ -231,17 +350,74 @@ suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
", may wakeup" : "");
}
/**
* suspend_device_late - Shut down one device (late suspend).
* @dev: Device.
* @state: Power state device is entering.
*
* This is called with interrupts off and only a single CPU running.
*/
static int suspend_device_late(struct device *dev, pm_message_t state)
{
int error = 0;
if (dev->bus && dev->bus->suspend_late) {
suspend_device_dbg(dev, state, "LATE ");
error = dev->bus->suspend_late(dev, state);
suspend_report_result(dev->bus->suspend_late, error);
}
return error;
}
/**
* device_power_down - Shut down special devices.
* @state: Power state to enter.
*
* Power down devices that require interrupts to be disabled
* and move them from the dpm_off list to the dpm_off_irq list.
* Then power down system devices.
*
* Must be called with interrupts disabled and only one CPU running.
*/
int device_power_down(pm_message_t state)
{
int error = 0;
while (!list_empty(&dpm_off)) {
struct list_head *entry = dpm_off.prev;
struct device *dev = to_device(entry);
list_del_init(&dev->power.entry);
error = suspend_device_late(dev, state);
if (error) {
printk(KERN_ERR "Could not power down device %s: "
"error %d\n",
kobject_name(&dev->kobj), error);
if (list_empty(&dev->power.entry))
list_add(&dev->power.entry, &dpm_off);
break;
}
if (list_empty(&dev->power.entry))
list_add(&dev->power.entry, &dpm_off_irq);
}
if (!error)
error = sysdev_suspend(state);
if (error)
dpm_power_up();
return error;
}
EXPORT_SYMBOL_GPL(device_power_down);
/**
* suspend_device - Save state of one device.
* @dev: Device.
* @state: Power state device is entering.
*/
static int suspend_device(struct device * dev, pm_message_t state)
int suspend_device(struct device *dev, pm_message_t state)
{
int error = 0;
down(&dev->sem);
if (dev->power.power_state.event) {
dev_dbg(dev, "PM: suspend %d-->%d\n",
dev->power.power_state.event, state.event);
@ -264,123 +440,105 @@ static int suspend_device(struct device * dev, pm_message_t state)
error = dev->bus->suspend(dev, state);
suspend_report_result(dev->bus->suspend, error);
}
up(&dev->sem);
return error;
}
/*
* This is called with interrupts off, only a single CPU
* running. We can't acquire a mutex or semaphore (and we don't
* need the protection)
*/
static int suspend_device_late(struct device *dev, pm_message_t state)
{
int error = 0;
if (dev->bus && dev->bus->suspend_late) {
suspend_device_dbg(dev, state, "LATE ");
error = dev->bus->suspend_late(dev, state);
suspend_report_result(dev->bus->suspend_late, error);
}
return error;
}
/**
* device_suspend - Save state and stop all devices in system.
* @state: Power state to put each device in.
* dpm_suspend - Suspend every device.
* @state: Power state to put each device in.
*
* Walk the dpm_active list, call ->suspend() for each device, and move
* it to the dpm_off list.
* Walk the dpm_locked list. Suspend each device and move it
* to the dpm_off list.
*
* (For historical reasons, if it returns -EAGAIN, that used to mean
* that the device would be called again with interrupts disabled.
* These days, we use the "suspend_late()" callback for that, so we
* print a warning and consider it an error).
*
* If we get a different error, try and back out.
*
* If we hit a failure with any of the devices, call device_resume()
* above to bring the suspended devices back to life.
*
*/
int device_suspend(pm_message_t state)
static int dpm_suspend(pm_message_t state)
{
int error = 0;
might_sleep();
mutex_lock(&dpm_mtx);
mutex_lock(&dpm_list_mtx);
while (!list_empty(&dpm_active) && error == 0) {
struct list_head * entry = dpm_active.prev;
struct device * dev = to_device(entry);
while (!list_empty(&dpm_locked)) {
struct list_head *entry = dpm_locked.prev;
struct device *dev = to_device(entry);
list_del_init(&dev->power.entry);
mutex_unlock(&dpm_list_mtx);
error = suspend_device(dev, state);
if (error) {
printk(KERN_ERR "Could not suspend device %s: "
"error %d%s\n",
kobject_name(&dev->kobj),
error,
(error == -EAGAIN ?
" (please convert to suspend_late)" :
""));
mutex_lock(&dpm_list_mtx);
if (list_empty(&dev->power.entry))
list_add(&dev->power.entry, &dpm_locked);
mutex_unlock(&dpm_list_mtx);
break;
}
mutex_lock(&dpm_list_mtx);
if (list_empty(&dev->power.entry))
list_add(&dev->power.entry, &dpm_off);
}
mutex_unlock(&dpm_list_mtx);
return error;
}
/**
* lock_all_devices - Acquire every device's semaphore
*
* Go through the dpm_active list. Carefully lock each device's
* semaphore and put it in on the dpm_locked list.
*/
static void lock_all_devices(void)
{
mutex_lock(&dpm_list_mtx);
while (!list_empty(&dpm_active)) {
struct list_head *entry = dpm_active.next;
struct device *dev = to_device(entry);
/* Required locking order is dev->sem first,
* then dpm_list_mutex. Hence this awkward code.
*/
get_device(dev);
mutex_unlock(&dpm_list_mtx);
error = suspend_device(dev, state);
down(&dev->sem);
mutex_lock(&dpm_list_mtx);
/* Check if the device got removed */
if (!list_empty(&dev->power.entry)) {
/* Move it to the dpm_off list */
if (!error)
list_move(&dev->power.entry, &dpm_off);
}
if (error)
printk(KERN_ERR "Could not suspend device %s: "
"error %d%s\n",
kobject_name(&dev->kobj), error,
error == -EAGAIN ? " (please convert to suspend_late)" : "");
if (list_empty(entry))
up(&dev->sem); /* Device was removed */
else
list_move_tail(entry, &dpm_locked);
put_device(dev);
}
mutex_unlock(&dpm_list_mtx);
if (error)
dpm_resume();
mutex_unlock(&dpm_mtx);
return error;
}
EXPORT_SYMBOL_GPL(device_suspend);
/**
* device_power_down - Shut down special devices.
* @state: Power state to enter.
* device_suspend - Save state and stop all devices in system.
*
* Walk the dpm_off_irq list, calling ->power_down() for each device that
* couldn't power down the device with interrupts enabled. When we're
* done, power down system devices.
* Prevent new devices from being registered, then lock all devices
* and suspend them.
*/
int device_power_down(pm_message_t state)
int device_suspend(pm_message_t state)
{
int error = 0;
struct device * dev;
int error;
while (!list_empty(&dpm_off)) {
struct list_head * entry = dpm_off.prev;
dev = to_device(entry);
error = suspend_device_late(dev, state);
if (error)
goto Error;
list_move(&dev->power.entry, &dpm_off_irq);
}
error = sysdev_suspend(state);
Done:
might_sleep();
down_write(&pm_sleep_rwsem);
lock_all_devices();
error = dpm_suspend(state);
if (error)
device_resume();
return error;
Error:
printk(KERN_ERR "Could not power down device %s: "
"error %d\n", kobject_name(&dev->kobj), error);
dpm_power_up();
goto Done;
}
EXPORT_SYMBOL_GPL(device_power_down);
EXPORT_SYMBOL_GPL(device_suspend);
void __suspend_report_result(const char *function, void *fn, int ret)
{

View file

@ -1,10 +1,3 @@
/*
* shutdown.c
*/
extern void device_shutdown(void);
#ifdef CONFIG_PM_SLEEP
/*
@ -20,6 +13,9 @@ static inline struct device *to_device(struct list_head *entry)
extern void device_pm_add(struct device *);
extern void device_pm_remove(struct device *);
extern void device_pm_schedule_removal(struct device *);
extern int pm_sleep_lock(void);
extern void pm_sleep_unlock(void);
#else /* CONFIG_PM_SLEEP */
@ -32,6 +28,15 @@ static inline void device_pm_remove(struct device *dev)
{
}
static inline int pm_sleep_lock(void)
{
return 0;
}
static inline void pm_sleep_unlock(void)
{
}
#endif
#ifdef CONFIG_PM

View file

@ -1,48 +0,0 @@
/*
* shutdown.c - power management functions for the device tree.
*
* Copyright (c) 2002-3 Patrick Mochel
* 2002-3 Open Source Development Lab
*
* This file is released under the GPLv2
*
*/
#include <linux/device.h>
#include <asm/semaphore.h>
#include "../base.h"
#include "power.h"
#define to_dev(node) container_of(node, struct device, kobj.entry)
/**
* We handle system devices differently - we suspend and shut them
* down last and resume them first. That way, we don't do anything stupid like
* shutting down the interrupt controller before any devices..
*
* Note that there are not different stages for power management calls -
* they only get one called once when interrupts are disabled.
*/
/**
* device_shutdown - call ->shutdown() on each device to shutdown.
*/
void device_shutdown(void)
{
struct device * dev, *devn;
list_for_each_entry_safe_reverse(dev, devn, &devices_subsys.list,
kobj.entry) {
if (dev->bus && dev->bus->shutdown) {
dev_dbg(dev, "shutdown\n");
dev->bus->shutdown(dev);
} else if (dev->driver && dev->driver->shutdown) {
dev_dbg(dev, "shutdown\n");
dev->driver->shutdown(dev);
}
}
}

View file

@ -25,8 +25,6 @@
#include "base.h"
extern struct kset devices_subsys;
#define to_sysdev(k) container_of(k, struct sys_device, kobj)
#define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr)
@ -128,18 +126,17 @@ void sysdev_class_remove_file(struct sysdev_class *c,
}
EXPORT_SYMBOL_GPL(sysdev_class_remove_file);
/*
* declare system_subsys
*/
static decl_subsys(system, &ktype_sysdev_class, NULL);
static struct kset *system_kset;
int sysdev_class_register(struct sysdev_class * cls)
{
pr_debug("Registering sysdev class '%s'\n",
kobject_name(&cls->kset.kobj));
INIT_LIST_HEAD(&cls->drivers);
cls->kset.kobj.parent = &system_subsys.kobj;
cls->kset.kobj.kset = &system_subsys;
cls->kset.kobj.parent = &system_kset->kobj;
cls->kset.kobj.ktype = &ktype_sysdev_class;
cls->kset.kobj.kset = system_kset;
kobject_set_name(&cls->kset.kobj, cls->name);
return kset_register(&cls->kset);
}
@ -228,20 +225,15 @@ int sysdev_register(struct sys_device * sysdev)
if (!cls)
return -EINVAL;
pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
/* Make sure the kset is set */
sysdev->kobj.kset = &cls->kset;
/* But make sure we point to the right type for sysfs translation */
sysdev->kobj.ktype = &ktype_sysdev;
error = kobject_set_name(&sysdev->kobj, "%s%d",
kobject_name(&cls->kset.kobj), sysdev->id);
if (error)
return error;
pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
/* Register the object */
error = kobject_register(&sysdev->kobj);
error = kobject_init_and_add(&sysdev->kobj, &ktype_sysdev, NULL,
"%s%d", kobject_name(&cls->kset.kobj),
sysdev->id);
if (!error) {
struct sysdev_driver * drv;
@ -258,6 +250,7 @@ int sysdev_register(struct sys_device * sysdev)
}
mutex_unlock(&sysdev_drivers_lock);
}
kobject_uevent(&sysdev->kobj, KOBJ_ADD);
return error;
}
@ -272,7 +265,7 @@ void sysdev_unregister(struct sys_device * sysdev)
}
mutex_unlock(&sysdev_drivers_lock);
kobject_unregister(&sysdev->kobj);
kobject_put(&sysdev->kobj);
}
@ -298,8 +291,7 @@ void sysdev_shutdown(void)
pr_debug("Shutting Down System Devices\n");
mutex_lock(&sysdev_drivers_lock);
list_for_each_entry_reverse(cls, &system_subsys.list,
kset.kobj.entry) {
list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
struct sys_device * sysdev;
pr_debug("Shutting down type '%s':\n",
@ -361,9 +353,7 @@ int sysdev_suspend(pm_message_t state)
pr_debug("Suspending System Devices\n");
list_for_each_entry_reverse(cls, &system_subsys.list,
kset.kobj.entry) {
list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
pr_debug("Suspending type '%s':\n",
kobject_name(&cls->kset.kobj));
@ -414,8 +404,7 @@ int sysdev_suspend(pm_message_t state)
}
/* resume other classes */
list_for_each_entry_continue(cls, &system_subsys.list,
kset.kobj.entry) {
list_for_each_entry_continue(cls, &system_kset->list, kset.kobj.entry) {
list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
pr_debug(" %s\n", kobject_name(&err_dev->kobj));
__sysdev_resume(err_dev);
@ -440,7 +429,7 @@ int sysdev_resume(void)
pr_debug("Resuming System Devices\n");
list_for_each_entry(cls, &system_subsys.list, kset.kobj.entry) {
list_for_each_entry(cls, &system_kset->list, kset.kobj.entry) {
struct sys_device * sysdev;
pr_debug("Resuming type '%s':\n",
@ -458,8 +447,10 @@ int sysdev_resume(void)
int __init system_bus_init(void)
{
system_subsys.kobj.parent = &devices_subsys.kobj;
return subsystem_register(&system_subsys);
system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
if (!system_kset)
return -ENOMEM;
return 0;
}
EXPORT_SYMBOL_GPL(sysdev_register);

View file

@ -15,8 +15,10 @@
static struct kmem_cache *buf_pool_cache;
static ssize_t aoedisk_show_state(struct gendisk * disk, char *page)
static ssize_t aoedisk_show_state(struct device *dev,
struct device_attribute *attr, char *page)
{
struct gendisk *disk = dev_to_disk(dev);
struct aoedev *d = disk->private_data;
return snprintf(page, PAGE_SIZE,
@ -26,50 +28,47 @@ static ssize_t aoedisk_show_state(struct gendisk * disk, char *page)
(d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
/* I'd rather see nopen exported so we can ditch closewait */
}
static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page)
static ssize_t aoedisk_show_mac(struct device *dev,
struct device_attribute *attr, char *page)
{
struct gendisk *disk = dev_to_disk(dev);
struct aoedev *d = disk->private_data;
return snprintf(page, PAGE_SIZE, "%012llx\n",
(unsigned long long)mac_addr(d->addr));
}
static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page)
static ssize_t aoedisk_show_netif(struct device *dev,
struct device_attribute *attr, char *page)
{
struct gendisk *disk = dev_to_disk(dev);
struct aoedev *d = disk->private_data;
return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
}
/* firmware version */
static ssize_t aoedisk_show_fwver(struct gendisk * disk, char *page)
static ssize_t aoedisk_show_fwver(struct device *dev,
struct device_attribute *attr, char *page)
{
struct gendisk *disk = dev_to_disk(dev);
struct aoedev *d = disk->private_data;
return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
}
static struct disk_attribute disk_attr_state = {
.attr = {.name = "state", .mode = S_IRUGO },
.show = aoedisk_show_state
};
static struct disk_attribute disk_attr_mac = {
.attr = {.name = "mac", .mode = S_IRUGO },
.show = aoedisk_show_mac
};
static struct disk_attribute disk_attr_netif = {
.attr = {.name = "netif", .mode = S_IRUGO },
.show = aoedisk_show_netif
};
static struct disk_attribute disk_attr_fwver = {
.attr = {.name = "firmware-version", .mode = S_IRUGO },
.show = aoedisk_show_fwver
static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
static struct device_attribute dev_attr_firmware_version = {
.attr = { .name = "firmware-version", .mode = S_IRUGO, .owner = THIS_MODULE },
.show = aoedisk_show_fwver,
};
static struct attribute *aoe_attrs[] = {
&disk_attr_state.attr,
&disk_attr_mac.attr,
&disk_attr_netif.attr,
&disk_attr_fwver.attr,
NULL
&dev_attr_state.attr,
&dev_attr_mac.attr,
&dev_attr_netif.attr,
&dev_attr_firmware_version.attr,
NULL,
};
static const struct attribute_group attr_group = {
@ -79,12 +78,12 @@ static const struct attribute_group attr_group = {
static int
aoedisk_add_sysfs(struct aoedev *d)
{
return sysfs_create_group(&d->gd->kobj, &attr_group);
return sysfs_create_group(&d->gd->dev.kobj, &attr_group);
}
void
aoedisk_rm_sysfs(struct aoedev *d)
{
sysfs_remove_group(&d->gd->kobj, &attr_group);
sysfs_remove_group(&d->gd->dev.kobj, &attr_group);
}
static int

View file

@ -259,9 +259,8 @@ aoechr_init(void)
return PTR_ERR(aoe_class);
}
for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
class_device_create(aoe_class, NULL,
MKDEV(AOE_MAJOR, chardevs[i].minor),
NULL, chardevs[i].name);
device_create(aoe_class, NULL,
MKDEV(AOE_MAJOR, chardevs[i].minor), chardevs[i].name);
return 0;
}
@ -272,7 +271,7 @@ aoechr_exit(void)
int i;
for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
class_device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
class_destroy(aoe_class);
unregister_chrdev(AOE_MAJOR, "aoechr");
}

View file

@ -375,14 +375,17 @@ static struct request *nbd_read_stat(struct nbd_device *lo)
return NULL;
}
static ssize_t pid_show(struct gendisk *disk, char *page)
static ssize_t pid_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(page, "%ld\n",
struct gendisk *disk = dev_to_disk(dev);
return sprintf(buf, "%ld\n",
(long) ((struct nbd_device *)disk->private_data)->pid);
}
static struct disk_attribute pid_attr = {
.attr = { .name = "pid", .mode = S_IRUGO },
static struct device_attribute pid_attr = {
.attr = { .name = "pid", .mode = S_IRUGO, .owner = THIS_MODULE },
.show = pid_show,
};
@ -394,7 +397,7 @@ static int nbd_do_it(struct nbd_device *lo)
BUG_ON(lo->magic != LO_MAGIC);
lo->pid = current->pid;
ret = sysfs_create_file(&lo->disk->kobj, &pid_attr.attr);
ret = sysfs_create_file(&lo->disk->dev.kobj, &pid_attr.attr);
if (ret) {
printk(KERN_ERR "nbd: sysfs_create_file failed!");
return ret;
@ -403,7 +406,7 @@ static int nbd_do_it(struct nbd_device *lo)
while ((req = nbd_read_stat(lo)) != NULL)
nbd_end_request(req);
sysfs_remove_file(&lo->disk->kobj, &pid_attr.attr);
sysfs_remove_file(&lo->disk->dev.kobj, &pid_attr.attr);
return 0;
}

View file

@ -676,8 +676,8 @@ static int __init pg_init(void)
for (unit = 0; unit < PG_UNITS; unit++) {
struct pg *dev = &devices[unit];
if (dev->present)
class_device_create(pg_class, NULL, MKDEV(major, unit),
NULL, "pg%u", unit);
device_create(pg_class, NULL, MKDEV(major, unit),
"pg%u", unit);
}
err = 0;
goto out;
@ -695,7 +695,7 @@ static void __exit pg_exit(void)
for (unit = 0; unit < PG_UNITS; unit++) {
struct pg *dev = &devices[unit];
if (dev->present)
class_device_destroy(pg_class, MKDEV(major, unit));
device_destroy(pg_class, MKDEV(major, unit));
}
class_destroy(pg_class);
unregister_chrdev(major, name);

View file

@ -972,10 +972,10 @@ static int __init pt_init(void)
for (unit = 0; unit < PT_UNITS; unit++)
if (pt[unit].present) {
class_device_create(pt_class, NULL, MKDEV(major, unit),
NULL, "pt%d", unit);
class_device_create(pt_class, NULL, MKDEV(major, unit + 128),
NULL, "pt%dn", unit);
device_create(pt_class, NULL, MKDEV(major, unit),
"pt%d", unit);
device_create(pt_class, NULL, MKDEV(major, unit + 128),
"pt%dn", unit);
}
goto out;
@ -990,8 +990,8 @@ static void __exit pt_exit(void)
int unit;
for (unit = 0; unit < PT_UNITS; unit++)
if (pt[unit].present) {
class_device_destroy(pt_class, MKDEV(major, unit));
class_device_destroy(pt_class, MKDEV(major, unit + 128));
device_destroy(pt_class, MKDEV(major, unit));
device_destroy(pt_class, MKDEV(major, unit + 128));
}
class_destroy(pt_class);
unregister_chrdev(major, name);

View file

@ -110,17 +110,18 @@ static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
struct kobj_type* ktype)
{
struct pktcdvd_kobj *p;
int error;
p = kzalloc(sizeof(*p), GFP_KERNEL);
if (!p)
return NULL;
kobject_set_name(&p->kobj, "%s", name);
p->kobj.parent = parent;
p->kobj.ktype = ktype;
p->pd = pd;
if (kobject_register(&p->kobj) != 0) {
error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
if (error) {
kobject_put(&p->kobj);
return NULL;
}
kobject_uevent(&p->kobj, KOBJ_ADD);
return p;
}
/*
@ -129,7 +130,7 @@ static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
static void pkt_kobj_remove(struct pktcdvd_kobj *p)
{
if (p)
kobject_unregister(&p->kobj);
kobject_put(&p->kobj);
}
/*
* default release function for pktcdvd kernel objects.
@ -301,18 +302,16 @@ static struct kobj_type kobj_pkt_type_wqueue = {
static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
{
if (class_pktcdvd) {
pd->clsdev = class_device_create(class_pktcdvd,
NULL, pd->pkt_dev,
NULL, "%s", pd->name);
if (IS_ERR(pd->clsdev))
pd->clsdev = NULL;
pd->dev = device_create(class_pktcdvd, NULL, pd->pkt_dev, "%s", pd->name);
if (IS_ERR(pd->dev))
pd->dev = NULL;
}
if (pd->clsdev) {
if (pd->dev) {
pd->kobj_stat = pkt_kobj_create(pd, "stat",
&pd->clsdev->kobj,
&pd->dev->kobj,
&kobj_pkt_type_stat);
pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
&pd->clsdev->kobj,
&pd->dev->kobj,
&kobj_pkt_type_wqueue);
}
}
@ -322,7 +321,7 @@ static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
pkt_kobj_remove(pd->kobj_stat);
pkt_kobj_remove(pd->kobj_wqueue);
if (class_pktcdvd)
class_device_destroy(class_pktcdvd, pd->pkt_dev);
device_destroy(class_pktcdvd, pd->pkt_dev);
}

View file

@ -373,6 +373,16 @@ config ISTALLION
To compile this driver as a module, choose M here: the
module will be called istallion.
config NOZOMI
tristate "HSDPA Broadband Wireless Data Card - Globe Trotter"
depends on PCI && EXPERIMENTAL
help
If you have a HSDPA driver Broadband Wireless Data Card -
Globe Trotter PCMCIA card, say Y here.
To compile this driver as a module, choose M here, the module
will be called nozomi.
config A2232
tristate "Commodore A2232 serial support (EXPERIMENTAL)"
depends on EXPERIMENTAL && ZORRO && BROKEN_ON_SMP

View file

@ -26,6 +26,7 @@ obj-$(CONFIG_SERIAL167) += serial167.o
obj-$(CONFIG_CYCLADES) += cyclades.o
obj-$(CONFIG_STALLION) += stallion.o
obj-$(CONFIG_ISTALLION) += istallion.o
obj-$(CONFIG_NOZOMI) += nozomi.o
obj-$(CONFIG_DIGIEPCA) += epca.o
obj-$(CONFIG_SPECIALIX) += specialix.o
obj-$(CONFIG_MOXA_INTELLIO) += moxa.o

View file

@ -27,7 +27,7 @@
#include <linux/init.h>
#include <linux/kbd_kern.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/kref.h>
#include <linux/kthread.h>
#include <linux/list.h>
#include <linux/module.h>
@ -89,7 +89,7 @@ struct hvc_struct {
int irq_requested;
int irq;
struct list_head next;
struct kobject kobj; /* ref count & hvc_struct lifetime */
struct kref kref; /* ref count & hvc_struct lifetime */
};
/* dynamic list of hvc_struct instances */
@ -110,7 +110,7 @@ static int last_hvc = -1;
/*
* Do not call this function with either the hvc_structs_lock or the hvc_struct
* lock held. If successful, this function increments the kobject reference
* lock held. If successful, this function increments the kref reference
* count against the target hvc_struct so it should be released when finished.
*/
static struct hvc_struct *hvc_get_by_index(int index)
@ -123,7 +123,7 @@ static struct hvc_struct *hvc_get_by_index(int index)
list_for_each_entry(hp, &hvc_structs, next) {
spin_lock_irqsave(&hp->lock, flags);
if (hp->index == index) {
kobject_get(&hp->kobj);
kref_get(&hp->kref);
spin_unlock_irqrestore(&hp->lock, flags);
spin_unlock(&hvc_structs_lock);
return hp;
@ -242,6 +242,23 @@ static int __init hvc_console_init(void)
}
console_initcall(hvc_console_init);
/* callback when the kboject ref count reaches zero. */
static void destroy_hvc_struct(struct kref *kref)
{
struct hvc_struct *hp = container_of(kref, struct hvc_struct, kref);
unsigned long flags;
spin_lock(&hvc_structs_lock);
spin_lock_irqsave(&hp->lock, flags);
list_del(&(hp->next));
spin_unlock_irqrestore(&hp->lock, flags);
spin_unlock(&hvc_structs_lock);
kfree(hp);
}
/*
* hvc_instantiate() is an early console discovery method which locates
* 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)
/* make sure no no tty has been registered in this index */
hp = hvc_get_by_index(index);
if (hp) {
kobject_put(&hp->kobj);
kref_put(&hp->kref, destroy_hvc_struct);
return -1;
}
@ -318,9 +335,8 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
unsigned long flags;
int irq = 0;
int rc = 0;
struct kobject *kobjp;
/* Auto increments kobject reference if found. */
/* Auto increments kref reference if found. */
if (!(hp = hvc_get_by_index(tty->index)))
return -ENODEV;
@ -341,8 +357,6 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
if (irq)
hp->irq_requested = 1;
kobjp = &hp->kobj;
spin_unlock_irqrestore(&hp->lock, flags);
/* check error, fallback to non-irq */
if (irq)
@ -352,7 +366,7 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
* If the request_irq() fails and we return an error. The tty layer
* will call hvc_close() after a failed open but we don't want to clean
* up there so we'll clean up here and clear out the previously set
* tty fields and return the kobject reference.
* tty fields and return the kref reference.
*/
if (rc) {
spin_lock_irqsave(&hp->lock, flags);
@ -360,7 +374,7 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
hp->irq_requested = 0;
spin_unlock_irqrestore(&hp->lock, flags);
tty->driver_data = NULL;
kobject_put(kobjp);
kref_put(&hp->kref, destroy_hvc_struct);
printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
}
/* Force wakeup of the polling thread */
@ -372,7 +386,6 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
static void hvc_close(struct tty_struct *tty, struct file * filp)
{
struct hvc_struct *hp;
struct kobject *kobjp;
int irq = 0;
unsigned long flags;
@ -382,7 +395,7 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
/*
* No driver_data means that this close was issued after a failed
* hvc_open by the tty layer's release_dev() function and we can just
* exit cleanly because the kobject reference wasn't made.
* exit cleanly because the kref reference wasn't made.
*/
if (!tty->driver_data)
return;
@ -390,7 +403,6 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
hp = tty->driver_data;
spin_lock_irqsave(&hp->lock, flags);
kobjp = &hp->kobj;
if (--hp->count == 0) {
if (hp->irq_requested)
irq = hp->irq;
@ -417,7 +429,7 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
spin_unlock_irqrestore(&hp->lock, flags);
}
kobject_put(kobjp);
kref_put(&hp->kref, destroy_hvc_struct);
}
static void hvc_hangup(struct tty_struct *tty)
@ -426,7 +438,6 @@ static void hvc_hangup(struct tty_struct *tty)
unsigned long flags;
int irq = 0;
int temp_open_count;
struct kobject *kobjp;
if (!hp)
return;
@ -443,7 +454,6 @@ static void hvc_hangup(struct tty_struct *tty)
return;
}
kobjp = &hp->kobj;
temp_open_count = hp->count;
hp->count = 0;
hp->n_outbuf = 0;
@ -457,7 +467,7 @@ static void hvc_hangup(struct tty_struct *tty)
free_irq(irq, hp);
while(temp_open_count) {
--temp_open_count;
kobject_put(kobjp);
kref_put(&hp->kref, destroy_hvc_struct);
}
}
@ -729,27 +739,6 @@ static const struct tty_operations hvc_ops = {
.chars_in_buffer = hvc_chars_in_buffer,
};
/* callback when the kboject ref count reaches zero. */
static void destroy_hvc_struct(struct kobject *kobj)
{
struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
unsigned long flags;
spin_lock(&hvc_structs_lock);
spin_lock_irqsave(&hp->lock, flags);
list_del(&(hp->next));
spin_unlock_irqrestore(&hp->lock, flags);
spin_unlock(&hvc_structs_lock);
kfree(hp);
}
static struct kobj_type hvc_kobj_type = {
.release = destroy_hvc_struct,
};
struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
struct hv_ops *ops, int outbuf_size)
{
@ -776,8 +765,7 @@ struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
hp->outbuf_size = outbuf_size;
hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
kobject_init(&hp->kobj);
hp->kobj.ktype = &hvc_kobj_type;
kref_init(&hp->kref);
spin_lock_init(&hp->lock);
spin_lock(&hvc_structs_lock);
@ -806,12 +794,10 @@ struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
int __devexit hvc_remove(struct hvc_struct *hp)
{
unsigned long flags;
struct kobject *kobjp;
struct tty_struct *tty;
spin_lock_irqsave(&hp->lock, flags);
tty = hp->tty;
kobjp = &hp->kobj;
if (hp->index < MAX_NR_HVC_CONSOLES)
vtermnos[hp->index] = -1;
@ -821,12 +807,12 @@ int __devexit hvc_remove(struct hvc_struct *hp)
spin_unlock_irqrestore(&hp->lock, flags);
/*
* We 'put' the instance that was grabbed when the kobject instance
* was initialized using kobject_init(). Let the last holder of this
* kobject cause it to be removed, which will probably be the tty_hangup
* We 'put' the instance that was grabbed when the kref instance
* was initialized using kref_init(). Let the last holder of this
* kref cause it to be removed, which will probably be the tty_hangup
* below.
*/
kobject_put(kobjp);
kref_put(&hp->kref, destroy_hvc_struct);
/*
* This function call will auto chain call hvc_hangup. The tty should

View file

@ -57,11 +57,7 @@
* rescanning partner information upon a user's request.
*
* Each vty-server, prior to being exposed to this driver is reference counted
* using the 2.6 Linux kernel kobject construct. This kobject is also used by
* the vio bus to provide a vio device sysfs entry that this driver attaches
* device specific attributes to, including partner information. The vio bus
* framework also provides a sysfs entry for each vio driver. The hvcs driver
* provides driver attributes in this entry.
* using the 2.6 Linux kernel kref construct.
*
* For direction on installation and usage of this driver please reference
* Documentation/powerpc/hvcs.txt.
@ -71,7 +67,7 @@
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/kref.h>
#include <linux/kthread.h>
#include <linux/list.h>
#include <linux/major.h>
@ -293,12 +289,12 @@ struct hvcs_struct {
int chars_in_buffer;
/*
* Any variable below the kobject is valid before a tty is connected and
* Any variable below the kref is valid before a tty is connected and
* stays valid after the tty is disconnected. These shouldn't be
* whacked until the koject refcount reaches zero though some entries
* may be changed via sysfs initiatives.
*/
struct kobject kobj; /* ref count & hvcs_struct lifetime */
struct kref kref; /* ref count & hvcs_struct lifetime */
int connected; /* is the vty-server currently connected to a vty? */
uint32_t p_unit_address; /* partner unit address */
uint32_t p_partition_ID; /* partner partition ID */
@ -307,8 +303,8 @@ struct hvcs_struct {
struct vio_dev *vdev;
};
/* Required to back map a kobject to its containing object */
#define from_kobj(kobj) container_of(kobj, struct hvcs_struct, kobj)
/* Required to back map a kref to its containing object */
#define from_kref(k) container_of(k, struct hvcs_struct, kref)
static struct list_head hvcs_structs = LIST_HEAD_INIT(hvcs_structs);
static DEFINE_SPINLOCK(hvcs_structs_lock);
@ -334,7 +330,6 @@ static void hvcs_partner_free(struct hvcs_struct *hvcsd);
static int hvcs_enable_device(struct hvcs_struct *hvcsd,
uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
static void destroy_hvcs_struct(struct kobject *kobj);
static int hvcs_open(struct tty_struct *tty, struct file *filp);
static void hvcs_close(struct tty_struct *tty, struct file *filp);
static void hvcs_hangup(struct tty_struct * tty);
@ -703,10 +698,10 @@ static void hvcs_return_index(int index)
hvcs_index_list[index] = -1;
}
/* callback when the kboject ref count reaches zero */
static void destroy_hvcs_struct(struct kobject *kobj)
/* callback when the kref ref count reaches zero */
static void destroy_hvcs_struct(struct kref *kref)
{
struct hvcs_struct *hvcsd = from_kobj(kobj);
struct hvcs_struct *hvcsd = from_kref(kref);
struct vio_dev *vdev;
unsigned long flags;
@ -743,10 +738,6 @@ static void destroy_hvcs_struct(struct kobject *kobj)
kfree(hvcsd);
}
static struct kobj_type hvcs_kobj_type = {
.release = destroy_hvcs_struct,
};
static int hvcs_get_index(void)
{
int i;
@ -791,9 +782,7 @@ static int __devinit hvcs_probe(
spin_lock_init(&hvcsd->lock);
/* Automatically incs the refcount the first time */
kobject_init(&hvcsd->kobj);
/* Set up the callback for terminating the hvcs_struct's life */
hvcsd->kobj.ktype = &hvcs_kobj_type;
kref_init(&hvcsd->kref);
hvcsd->vdev = dev;
dev->dev.driver_data = hvcsd;
@ -844,7 +833,6 @@ static int __devexit hvcs_remove(struct vio_dev *dev)
{
struct hvcs_struct *hvcsd = dev->dev.driver_data;
unsigned long flags;
struct kobject *kobjp;
struct tty_struct *tty;
if (!hvcsd)
@ -856,15 +844,13 @@ static int __devexit hvcs_remove(struct vio_dev *dev)
tty = hvcsd->tty;
kobjp = &hvcsd->kobj;
spin_unlock_irqrestore(&hvcsd->lock, flags);
/*
* Let the last holder of this object cause it to be removed, which
* would probably be tty_hangup below.
*/
kobject_put (kobjp);
kref_put(&hvcsd->kref, destroy_hvcs_struct);
/*
* 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,
}
/*
* This always increments the kobject ref count if the call is successful.
* This always increments the kref ref count if the call is successful.
* Please remember to dec when you are done with the instance.
*
* 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)
list_for_each_entry(hvcsd, &hvcs_structs, next) {
spin_lock_irqsave(&hvcsd->lock, flags);
if (hvcsd->index == index) {
kobject_get(&hvcsd->kobj);
kref_get(&hvcsd->kref);
spin_unlock_irqrestore(&hvcsd->lock, flags);
spin_unlock(&hvcs_structs_lock);
return hvcsd;
@ -1129,14 +1115,13 @@ static int hvcs_open(struct tty_struct *tty, struct file *filp)
unsigned int irq;
struct vio_dev *vdev;
unsigned long unit_address;
struct kobject *kobjp;
if (tty->driver_data)
goto fast_open;
/*
* Is there a vty-server that shares the same index?
* This function increments the kobject index.
* This function increments the kref index.
*/
if (!(hvcsd = hvcs_get_by_index(tty->index))) {
printk(KERN_WARNING "HVCS: open failed, no device associated"
@ -1181,7 +1166,7 @@ static int hvcs_open(struct tty_struct *tty, struct file *filp)
* and will grab the spinlock and free the connection if it fails.
*/
if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
kobject_put(&hvcsd->kobj);
kref_put(&hvcsd->kref, destroy_hvcs_struct);
printk(KERN_WARNING "HVCS: enable device failed.\n");
return rc;
}
@ -1192,17 +1177,11 @@ static int hvcs_open(struct tty_struct *tty, struct file *filp)
hvcsd = tty->driver_data;
spin_lock_irqsave(&hvcsd->lock, flags);
if (!kobject_get(&hvcsd->kobj)) {
spin_unlock_irqrestore(&hvcsd->lock, flags);
printk(KERN_ERR "HVCS: Kobject of open"
" hvcs doesn't exist.\n");
return -EFAULT; /* Is this the right return value? */
}
kref_get(&hvcsd->kref);
hvcsd->open_count++;
hvcsd->todo_mask |= HVCS_SCHED_READ;
spin_unlock_irqrestore(&hvcsd->lock, flags);
open_success:
hvcs_kick();
@ -1212,9 +1191,8 @@ static int hvcs_open(struct tty_struct *tty, struct file *filp)
return 0;
error_release:
kobjp = &hvcsd->kobj;
spin_unlock_irqrestore(&hvcsd->lock, flags);
kobject_put(&hvcsd->kobj);
kref_put(&hvcsd->kref, destroy_hvcs_struct);
printk(KERN_WARNING "HVCS: partner connect failed.\n");
return retval;
@ -1224,7 +1202,6 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
{
struct hvcs_struct *hvcsd;
unsigned long flags;
struct kobject *kobjp;
int irq = NO_IRQ;
/*
@ -1245,7 +1222,6 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
hvcsd = tty->driver_data;
spin_lock_irqsave(&hvcsd->lock, flags);
kobjp = &hvcsd->kobj;
if (--hvcsd->open_count == 0) {
vio_disable_interrupts(hvcsd->vdev);
@ -1270,7 +1246,7 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
tty->driver_data = NULL;
free_irq(irq, hvcsd);
kobject_put(kobjp);
kref_put(&hvcsd->kref, destroy_hvcs_struct);
return;
} else if (hvcsd->open_count < 0) {
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)
}
spin_unlock_irqrestore(&hvcsd->lock, flags);
kobject_put(kobjp);
kref_put(&hvcsd->kref, destroy_hvcs_struct);
}
static void hvcs_hangup(struct tty_struct * tty)
@ -1287,21 +1263,17 @@ static void hvcs_hangup(struct tty_struct * tty)
struct hvcs_struct *hvcsd = tty->driver_data;
unsigned long flags;
int temp_open_count;
struct kobject *kobjp;
int irq = NO_IRQ;
spin_lock_irqsave(&hvcsd->lock, flags);
/* Preserve this so that we know how many kobject refs to put */
/* Preserve this so that we know how many kref refs to put */
temp_open_count = hvcsd->open_count;
/*
* Don't kobject put inside the spinlock because the destruction
* Don't kref put inside the spinlock because the destruction
* callback may use the spinlock and it may get called before the
* spinlock has been released. Get a pointer to the kobject and
* kobject_put on that after releasing the spinlock.
* spinlock has been released.
*/
kobjp = &hvcsd->kobj;
vio_disable_interrupts(hvcsd->vdev);
hvcsd->todo_mask = 0;
@ -1324,7 +1296,7 @@ static void hvcs_hangup(struct tty_struct * tty)
free_irq(irq, hvcsd);
/*
* We need to kobject_put() for every open_count we have since the
* We need to kref_put() for every open_count we have since the
* tty_hangup() function doesn't invoke a close per open connection on a
* non-console device.
*/
@ -1335,7 +1307,7 @@ static void hvcs_hangup(struct tty_struct * tty)
* NOTE: If this hangup was signaled from user space then the
* final put will never happen.
*/
kobject_put(kobjp);
kref_put(&hvcsd->kref, destroy_hvcs_struct);
}
}

Some files were not shown because too many files have changed in this diff Show more