Skip to content

Commit

Permalink
Driver core: move dev_get/set_drvdata to drivers/base/dd.c
Browse files Browse the repository at this point in the history
No one should directly access the driver_data field, so remove the field
and make it private.  We dynamically create the private field now if it
is needed, to handle drivers that call get/set before they are
registered with the driver core.

Also update the copyright notices on these files while we are there.

Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Greg Kroah-Hartman committed Sep 15, 2009
1 parent 2023c61 commit b402843
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
5 changes: 5 additions & 0 deletions drivers/base/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ struct class_private {
* @knode_parent - node in sibling list
* @knode_driver - node in driver list
* @knode_bus - node in bus list
* @driver_data - private pointer for driver specific info. Will turn into a
* list soon.
* @device - pointer back to the struct class that this structure is
* associated with.
*
Expand All @@ -80,6 +82,7 @@ struct device_private {
struct klist_node knode_parent;
struct klist_node knode_driver;
struct klist_node knode_bus;
void *driver_data;
struct device *device;
};
#define to_device_private_parent(obj) \
Expand All @@ -89,6 +92,8 @@ struct device_private {
#define to_device_private_bus(obj) \
container_of(obj, struct device_private, knode_bus)

extern int device_private_init(struct device *dev);

/* initialisation functions */
extern int devices_init(void);
extern int buses_init(void);
Expand Down
20 changes: 14 additions & 6 deletions drivers/base/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,17 @@ static void device_remove_sys_dev_entry(struct device *dev)
}
}

int device_private_init(struct device *dev)
{
dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
if (!dev->p)
return -ENOMEM;
dev->p->device = dev;
klist_init(&dev->p->klist_children, klist_children_get,
klist_children_put);
return 0;
}

/**
* device_add - add device to device hierarchy.
* @dev: device.
Expand All @@ -868,14 +879,11 @@ int device_add(struct device *dev)
if (!dev)
goto done;

dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
if (!dev->p) {
error = -ENOMEM;
goto done;
error = device_private_init(dev);
if (error)
goto done;
}
dev->p->device = dev;
klist_init(&dev->p->klist_children, klist_children_get,
klist_children_put);

/*
* for statically allocated devices, which should all be converted
Expand Down
31 changes: 29 additions & 2 deletions drivers/base/dd.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
*
* 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.
* Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
* Copyright (c) 2007-2009 Novell Inc.
*
* This file is released under the GPLv2
*/
Expand Down Expand Up @@ -391,3 +391,30 @@ void driver_detach(struct device_driver *drv)
put_device(dev);
}
}

/*
* These exports can't be _GPL due to .h files using this within them, and it
* might break something that was previously working...
*/
void *dev_get_drvdata(const struct device *dev)
{
if (dev && dev->p)
return dev->p->driver_data;
return NULL;
}
EXPORT_SYMBOL(dev_get_drvdata);

void dev_set_drvdata(struct device *dev, void *data)
{
int error;

if (!dev)
return;
if (!dev->p) {
error = device_private_init(dev);
if (error)
return;
}
dev->p->driver_data = data;
}
EXPORT_SYMBOL(dev_set_drvdata);
16 changes: 4 additions & 12 deletions include/linux/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* device.h - generic, centralized driver model
*
* Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
* Copyright (c) 2004-2007 Greg Kroah-Hartman <gregkh@suse.de>
* Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
* Copyright (c) 2008-2009 Novell Inc.
*
* This file is released under the GPLv2
*
Expand Down Expand Up @@ -381,7 +382,6 @@ struct device {
struct bus_type *bus; /* type of bus device is on */
struct device_driver *driver; /* which driver has allocated this
device */
void *driver_data; /* data private to the driver */
void *platform_data; /* Platform specific data, device
core doesn't touch it */
struct dev_pm_info power;
Expand Down Expand Up @@ -447,16 +447,6 @@ static inline void set_dev_node(struct device *dev, int node)
}
#endif

static inline void *dev_get_drvdata(const struct device *dev)
{
return dev->driver_data;
}

static inline void dev_set_drvdata(struct device *dev, void *data)
{
dev->driver_data = data;
}

static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
{
return dev->kobj.uevent_suppress;
Expand Down Expand Up @@ -490,6 +480,8 @@ extern int device_rename(struct device *dev, char *new_name);
extern int device_move(struct device *dev, struct device *new_parent,
enum dpm_order dpm_order);
extern const char *device_get_nodename(struct device *dev, const char **tmp);
extern void *dev_get_drvdata(const struct device *dev);
extern void dev_set_drvdata(struct device *dev, void *data);

/*
* Root device objects for grouping under /sys/devices
Expand Down

0 comments on commit b402843

Please sign in to comment.