blob: 1880208964d6c9a4a75d332927a0ef3e23b19191 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * device.h - generic, centralized driver model
3 *
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
Greg Kroah-Hartman89790fd2007-02-12 22:33:06 -08005 * Copyright (c) 2004-2007 Greg Kroah-Hartman <gregkh@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file is released under the GPLv2
8 *
9 * See Documentation/driver-model/ for more information.
10 */
11
12#ifndef _DEVICE_H_
13#define _DEVICE_H_
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/ioport.h>
16#include <linux/kobject.h>
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -080017#include <linux/klist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/list.h>
Andrew Morton4a7fb632006-08-14 22:43:17 -070019#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/types.h>
21#include <linux/module.h>
22#include <linux/pm.h>
23#include <asm/semaphore.h>
24#include <asm/atomic.h>
Benjamin Herrenschmidtc6dbaef2006-11-11 17:18:39 +110025#include <asm/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define DEVICE_NAME_SIZE 50
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080028/* DEVICE_NAME_HALF is really less than half to accommodate slop */
29#define DEVICE_NAME_HALF __stringify(20)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define DEVICE_ID_SIZE 32
31#define BUS_ID_SIZE KOBJ_NAME_LEN
32
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034struct device;
35struct device_driver;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080036struct driver_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct class;
38struct class_device;
Kay Sieversb8c5cec2007-02-16 17:33:36 +010039struct bus_type;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070040struct bus_type_private;
Kay Sieversb8c5cec2007-02-16 17:33:36 +010041
42struct bus_attribute {
43 struct attribute attr;
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080044 ssize_t (*show)(struct bus_type *bus, char *buf);
45 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
Kay Sieversb8c5cec2007-02-16 17:33:36 +010046};
47
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080048#define BUS_ATTR(_name, _mode, _show, _store) \
49struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
Kay Sieversb8c5cec2007-02-16 17:33:36 +010050
51extern int __must_check bus_create_file(struct bus_type *,
52 struct bus_attribute *);
53extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55struct bus_type {
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080056 const char *name;
57 struct bus_attribute *bus_attrs;
58 struct device_attribute *dev_attrs;
59 struct driver_attribute *drv_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080061 int (*match)(struct device *dev, struct device_driver *drv);
62 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
63 int (*probe)(struct device *dev);
64 int (*remove)(struct device *dev);
65 void (*shutdown)(struct device *dev);
Linus Torvalds7c8265f2006-06-24 14:50:29 -070066
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080067 int (*suspend)(struct device *dev, pm_message_t state);
68 int (*suspend_late)(struct device *dev, pm_message_t state);
69 int (*resume_early)(struct device *dev);
70 int (*resume)(struct device *dev);
Kay Sieversb8c5cec2007-02-16 17:33:36 +010071
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070072 struct bus_type_private *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080075extern int __must_check bus_register(struct bus_type *bus);
76extern void bus_unregister(struct bus_type *bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080078extern int __must_check bus_rescan_devices(struct bus_type *bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* iterator helpers for buses */
81
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080082int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
83 int (*fn)(struct device *dev, void *data));
84struct device *bus_find_device(struct bus_type *bus, struct device *start,
85 void *data,
86 int (*match)(struct device *dev, void *data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Andrew Morton4a7fb632006-08-14 22:43:17 -070088int __must_check bus_for_each_drv(struct bus_type *bus,
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -080089 struct device_driver *start, void *data,
90 int (*fn)(struct device_driver *, void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100092/*
93 * Bus notifiers: Get notified of addition/removal of devices
94 * and binding/unbinding of drivers to devices.
95 * In the long run, it should be a replacement for the platform
96 * notify hooks.
97 */
98struct notifier_block;
99
100extern int bus_register_notifier(struct bus_type *bus,
101 struct notifier_block *nb);
102extern int bus_unregister_notifier(struct bus_type *bus,
103 struct notifier_block *nb);
104
105/* All 4 notifers below get called with the target struct device *
106 * as an argument. Note that those functions are likely to be called
107 * with the device semaphore held in the core, so be careful.
108 */
109#define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
110#define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
111#define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
112#define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
113 unbound */
114
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700115extern struct kset *bus_get_kset(struct bus_type *bus);
Greg Kroah-Hartmanb2490722007-11-01 19:41:16 -0700116extern struct klist *bus_get_device_klist(struct bus_type *bus);
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118struct device_driver {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800119 const char *name;
120 struct bus_type *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800122 struct module *owner;
123 const char *mod_name; /* used for built-in modules */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800125 int (*probe) (struct device *dev);
126 int (*remove) (struct device *dev);
127 void (*shutdown) (struct device *dev);
128 int (*suspend) (struct device *dev, pm_message_t state);
129 int (*resume) (struct device *dev);
Cornelia Huck57c74532007-12-05 12:50:23 +0100130 struct attribute_group **groups;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800131
132 struct driver_private *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800136extern int __must_check driver_register(struct device_driver *drv);
137extern void driver_unregister(struct device_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800139extern struct device_driver *get_driver(struct device_driver *drv);
140extern void put_driver(struct device_driver *drv);
141extern struct device_driver *driver_find(const char *name,
142 struct bus_type *bus);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700143extern int driver_probe_done(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100145/* sysfs interface for exporting driver attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147struct driver_attribute {
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800148 struct attribute attr;
149 ssize_t (*show)(struct device_driver *driver, char *buf);
150 ssize_t (*store)(struct device_driver *driver, const char *buf,
151 size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800154#define DRIVER_ATTR(_name, _mode, _show, _store) \
155struct driver_attribute driver_attr_##_name = \
156 __ATTR(_name, _mode, _show, _store)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800158extern int __must_check driver_create_file(struct device_driver *driver,
159 struct driver_attribute *attr);
160extern void driver_remove_file(struct device_driver *driver,
161 struct driver_attribute *attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400163extern int __must_check driver_add_kobj(struct device_driver *drv,
164 struct kobject *kobj,
165 const char *fmt, ...);
166
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800167extern int __must_check driver_for_each_device(struct device_driver *drv,
168 struct device *start,
169 void *data,
170 int (*fn)(struct device *dev,
171 void *));
172struct device *driver_find_device(struct device_driver *drv,
173 struct device *start, void *data,
174 int (*match)(struct device *dev, void *data));
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -0800175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
177 * device classes
178 */
179struct class {
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800180 const char *name;
181 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700183 struct kset subsys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct list_head children;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700185 struct list_head devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct list_head interfaces;
Kay Sievers86406242007-03-14 03:25:56 +0100187 struct kset class_dirs;
Dave Youngfd048972008-01-22 15:27:08 +0800188 struct semaphore sem; /* locks children, devices, interfaces */
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800189 struct class_attribute *class_attrs;
190 struct class_device_attribute *class_dev_attrs;
191 struct device_attribute *dev_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800193 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
194 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800196 void (*release)(struct class_device *dev);
197 void (*class_release)(struct class *class);
198 void (*dev_release)(struct device *dev);
Linus Torvalds7c8265f2006-06-24 14:50:29 -0700199
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800200 int (*suspend)(struct device *dev, pm_message_t state);
201 int (*resume)(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800204extern int __must_check class_register(struct class *class);
205extern void class_unregister(struct class *class);
Dave Youngfd048972008-01-22 15:27:08 +0800206extern int class_for_each_device(struct class *class, void *data,
207 int (*fn)(struct device *dev, void *data));
208extern struct device *class_find_device(struct class *class, void *data,
209 int (*match)(struct device *, void *));
210extern struct class_device *class_find_child(struct class *class, void *data,
211 int (*match)(struct class_device *, void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214struct class_attribute {
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800215 struct attribute attr;
216 ssize_t (*show)(struct class *class, char *buf);
217 ssize_t (*store)(struct class *class, const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800220#define CLASS_ATTR(_name, _mode, _show, _store) \
221struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800223extern int __must_check class_create_file(struct class *class,
224 const struct class_attribute *attr);
225extern void class_remove_file(struct class *class,
226 const struct class_attribute *attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Kay Sieversa7fd6702005-10-01 14:49:43 +0200228struct class_device_attribute {
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800229 struct attribute attr;
230 ssize_t (*show)(struct class_device *, char *buf);
231 ssize_t (*store)(struct class_device *, const char *buf, size_t count);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200232};
233
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800234#define CLASS_DEVICE_ATTR(_name, _mode, _show, _store) \
Kay Sieversa7fd6702005-10-01 14:49:43 +0200235struct class_device_attribute class_device_attr_##_name = \
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800236 __ATTR(_name, _mode, _show, _store)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200237
Andrew Morton4a7fb632006-08-14 22:43:17 -0700238extern int __must_check class_device_create_file(struct class_device *,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200239 const struct class_device_attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Greg Kroah-Hartman74be2272005-10-27 22:25:43 -0700241/**
242 * struct class_device - class devices
243 * @class: pointer to the parent class for this class device. This is required.
244 * @devt: for internal use by the driver core only.
245 * @node: for internal use by the driver core only.
246 * @kobj: for internal use by the driver core only.
Stephen Hemminger14982212006-05-06 17:55:11 -0700247 * @groups: optional additional groups to be created
Greg Kroah-Hartman74be2272005-10-27 22:25:43 -0700248 * @dev: if set, a symlink to the struct device is created in the sysfs
249 * directory for this struct class device.
250 * @class_data: pointer to whatever you want to store here for this struct
251 * class_device. Use class_get_devdata() and class_set_devdata() to get and
252 * set this pointer.
253 * @parent: pointer to a struct class_device that is the parent of this struct
254 * class_device. If NULL, this class_device will show up at the root of the
255 * struct class in sysfs (which is probably what you want to have happen.)
256 * @release: pointer to a release function for this struct class_device. If
257 * set, this will be called instead of the class specific release function.
258 * Only use this if you want to override the default release function, like
259 * when you are nesting class_device structures.
Kay Sievers312c0042005-11-16 09:00:00 +0100260 * @uevent: pointer to a uevent function for this struct class_device. If
261 * set, this will be called instead of the class specific uevent function.
262 * Only use this if you want to override the default uevent function, like
Greg Kroah-Hartman74be2272005-10-27 22:25:43 -0700263 * when you are nesting class_device structures.
264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265struct class_device {
266 struct list_head node;
267
268 struct kobject kobj;
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800269 struct class *class;
270 dev_t devt;
271 struct device *dev;
272 void *class_data;
273 struct class_device *parent;
274 struct attribute_group **groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800276 void (*release)(struct class_device *dev);
277 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
278 char class_id[BUS_ID_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800281static inline void *class_get_devdata(struct class_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 return dev->class_data;
284}
285
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800286static inline void class_set_devdata(struct class_device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 dev->class_data = data;
289}
290
291
Andrew Morton4a7fb632006-08-14 22:43:17 -0700292extern int __must_check class_device_register(struct class_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293extern void class_device_unregister(struct class_device *);
294extern void class_device_initialize(struct class_device *);
Andrew Morton4a7fb632006-08-14 22:43:17 -0700295extern int __must_check class_device_add(struct class_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296extern void class_device_del(struct class_device *);
297
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800298extern struct class_device *class_device_get(struct class_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299extern void class_device_put(struct class_device *);
300
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800301extern void class_device_remove_file(struct class_device *,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 const struct class_device_attribute *);
Greg Kroah-Hartmand919fd42007-10-31 12:51:29 -0700303extern int __must_check class_device_create_bin_file(struct class_device *,
304 struct bin_attribute *);
305extern void class_device_remove_bin_file(struct class_device *,
306 struct bin_attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308struct class_interface {
309 struct list_head node;
310 struct class *class;
311
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500312 int (*add) (struct class_device *, struct class_interface *);
313 void (*remove) (struct class_device *, struct class_interface *);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200314 int (*add_dev) (struct device *, struct class_interface *);
315 void (*remove_dev) (struct device *, struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317
Andrew Morton4a7fb632006-08-14 22:43:17 -0700318extern int __must_check class_interface_register(struct class_interface *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319extern void class_interface_unregister(struct class_interface *);
320
Miguel Ojeda Sandonisab7d7372006-09-13 15:34:05 +0200321extern struct class *class_create(struct module *owner, const char *name);
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800322extern void class_destroy(struct class *cls);
Greg Kroah-Hartman51d172d2005-10-27 22:25:43 -0700323extern struct class_device *class_device_create(struct class *cls,
324 struct class_device *parent,
325 dev_t devt,
326 struct device *device,
Dmitry Torokhovddd5d352006-08-10 01:18:18 -0400327 const char *fmt, ...)
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800328 __attribute__((format(printf, 5, 6)));
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800329extern void class_device_destroy(struct class *cls, dev_t devt);
330
Kay Sievers414264f2007-03-12 21:08:57 +0100331/*
332 * The type of device, "struct device" is embedded in. A class
333 * or bus can contain devices of different types
334 * like "partitions" and "disks", "mouse" and "event".
335 * This identifies the device type and carries type-specific
336 * information, equivalent to the kobj_type of a kobject.
337 * If "name" is specified, the uevent will contain it in
338 * the DEVTYPE variable.
339 */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200340struct device_type {
Kay Sievers414264f2007-03-12 21:08:57 +0100341 const char *name;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500342 struct attribute_group **groups;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200343 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200344 void (*release)(struct device *dev);
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800345 int (*suspend)(struct device *dev, pm_message_t state);
346 int (*resume)(struct device *dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200347};
348
Kay Sieversa7fd6702005-10-01 14:49:43 +0200349/* interface for exporting device attributes */
350struct device_attribute {
351 struct attribute attr;
352 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
353 char *buf);
354 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
355 const char *buf, size_t count);
356};
357
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800358#define DEVICE_ATTR(_name, _mode, _show, _store) \
359struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200360
Andrew Morton4a7fb632006-08-14 22:43:17 -0700361extern int __must_check device_create_file(struct device *device,
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800362 struct device_attribute *entry);
363extern void device_remove_file(struct device *dev,
364 struct device_attribute *attr);
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700365extern int __must_check device_create_bin_file(struct device *dev,
366 struct bin_attribute *attr);
367extern void device_remove_bin_file(struct device *dev,
368 struct bin_attribute *attr);
Alan Stern523ded72007-04-26 00:12:04 -0700369extern int device_schedule_callback_owner(struct device *dev,
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800370 void (*func)(struct device *dev), struct module *owner);
Alan Stern523ded72007-04-26 00:12:04 -0700371
372/* This is a macro to avoid include problems with THIS_MODULE */
373#define device_schedule_callback(dev, func) \
374 device_schedule_callback_owner(dev, func, THIS_MODULE)
Tejun Heo9ac78492007-01-20 16:00:26 +0900375
376/* device resource management */
377typedef void (*dr_release_t)(struct device *dev, void *res);
378typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
379
380#ifdef CONFIG_DEBUG_DEVRES
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800381extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
Tejun Heo9ac78492007-01-20 16:00:26 +0900382 const char *name);
383#define devres_alloc(release, size, gfp) \
384 __devres_alloc(release, size, gfp, #release)
385#else
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800386extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
Tejun Heo9ac78492007-01-20 16:00:26 +0900387#endif
388extern void devres_free(void *res);
389extern void devres_add(struct device *dev, void *res);
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800390extern void *devres_find(struct device *dev, dr_release_t release,
Tejun Heo9ac78492007-01-20 16:00:26 +0900391 dr_match_t match, void *match_data);
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800392extern void *devres_get(struct device *dev, void *new_res,
393 dr_match_t match, void *match_data);
394extern void *devres_remove(struct device *dev, dr_release_t release,
395 dr_match_t match, void *match_data);
Tejun Heo9ac78492007-01-20 16:00:26 +0900396extern int devres_destroy(struct device *dev, dr_release_t release,
397 dr_match_t match, void *match_data);
398
399/* devres group */
400extern void * __must_check devres_open_group(struct device *dev, void *id,
401 gfp_t gfp);
402extern void devres_close_group(struct device *dev, void *id);
403extern void devres_remove_group(struct device *dev, void *id);
404extern int devres_release_group(struct device *dev, void *id);
405
406/* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
407extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
408extern void devm_kfree(struct device *dev, void *p);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410struct device {
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800411 struct klist klist_children;
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800412 struct klist_node knode_parent; /* node in sibling list */
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800413 struct klist_node knode_driver;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800414 struct klist_node knode_bus;
David Brownell49a4ec12007-05-08 00:29:39 -0700415 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 struct kobject kobj;
418 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200419 struct device_type *type;
Alan Sternf2eaae12006-09-18 16:22:34 -0400420 unsigned is_registered:1;
David Brownell49a4ec12007-05-08 00:29:39 -0700421 unsigned uevent_suppress:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800423 struct semaphore sem; /* semaphore to synchronize calls to
424 * its driver.
425 */
426
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800427 struct bus_type *bus; /* type of bus device is on */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct device_driver *driver; /* which driver has allocated this
429 device */
430 void *driver_data; /* data private to the driver */
David Shaohua Li4e10d122005-03-18 18:45:35 -0500431 void *platform_data; /* Platform specific data, device
432 core doesn't touch it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct dev_pm_info power;
434
Christoph Hellwig87348132006-12-06 20:32:33 -0800435#ifdef CONFIG_NUMA
436 int numa_node; /* NUMA node this device is close to */
437#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 u64 *dma_mask; /* dma mask (if dma'able device) */
439 u64 coherent_dma_mask;/* Like dma_mask, but for
440 alloc_coherent mappings as
441 not all hardware supports
442 64 bit addresses for consistent
443 allocations such descriptors. */
444
445 struct list_head dma_pools; /* dma pools (if dma'ble) */
446
447 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
448 override */
Benjamin Herrenschmidtc6dbaef2006-11-11 17:18:39 +1100449 /* arch specific additions */
450 struct dev_archdata archdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Tejun Heo9ac78492007-01-20 16:00:26 +0900452 spinlock_t devres_lock;
453 struct list_head devres_head;
454
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700455 /* class_device migration path */
456 struct list_head node;
Kay Sieversb7a3e812006-10-07 21:54:55 +0200457 struct class *class;
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800458 dev_t devt; /* dev_t, creates the sysfs "dev" */
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700459 struct attribute_group **groups; /* optional groups */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700460
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800461 void (*release)(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Christoph Hellwig87348132006-12-06 20:32:33 -0800464#ifdef CONFIG_NUMA
465static inline int dev_to_node(struct device *dev)
466{
467 return dev->numa_node;
468}
469static inline void set_dev_node(struct device *dev, int node)
470{
471 dev->numa_node = node;
472}
473#else
474static inline int dev_to_node(struct device *dev)
475{
476 return -1;
477}
478static inline void set_dev_node(struct device *dev, int node)
479{
480}
481#endif
482
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800483static inline void *dev_get_drvdata(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 return dev->driver_data;
486}
487
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800488static inline void dev_set_drvdata(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 dev->driver_data = data;
491}
492
Daniel Ritzd305ef52005-09-22 00:47:24 -0700493static inline int device_is_registered(struct device *dev)
494{
Alan Sternf2eaae12006-09-18 16:22:34 -0400495 return dev->is_registered;
Daniel Ritzd305ef52005-09-22 00:47:24 -0700496}
497
Adrian Bunk1f217822006-12-19 13:01:28 -0800498void driver_init(void);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
501 * High level routines for use by the bus drivers
502 */
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800503extern int __must_check device_register(struct device *dev);
504extern void device_unregister(struct device *dev);
505extern void device_initialize(struct device *dev);
506extern int __must_check device_add(struct device *dev);
507extern void device_del(struct device *dev);
508extern int device_for_each_child(struct device *dev, void *data,
509 int (*fn)(struct device *dev, void *data));
510extern struct device *device_find_child(struct device *dev, void *data,
511 int (*match)(struct device *dev, void *data));
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700512extern int device_rename(struct device *dev, char *new_name);
Cornelia Huck8a824722006-11-20 17:07:51 +0100513extern int device_move(struct device *dev, struct device *new_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/*
516 * Manual binding of a device to driver. See drivers/base/bus.c
517 * for information on use.
518 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700519extern int __must_check device_bind_driver(struct device *dev);
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800520extern void device_release_driver(struct device *dev);
521extern int __must_check device_attach(struct device *dev);
Andrew Mortonf86db392006-08-14 22:43:20 -0700522extern int __must_check driver_attach(struct device_driver *drv);
523extern int __must_check device_reprobe(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700525/*
526 * Easy functions for dynamically creating devices on the fly
527 */
528extern struct device *device_create(struct class *cls, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400529 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800530 __attribute__((format(printf, 4, 5)));
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700531extern void device_destroy(struct class *cls, dev_t devt);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100532#ifdef CONFIG_PM_SLEEP
533extern void destroy_suspended_device(struct class *cls, dev_t devt);
534#else /* !CONFIG_PM_SLEEP */
535static inline void destroy_suspended_device(struct class *cls, dev_t devt)
536{
537 device_destroy(cls, devt);
538}
539#endif /* !CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*
542 * Platform "fixup" functions - allow the platform to have their say
543 * about devices and actions that the general device layer doesn't
544 * know about.
545 */
546/* Notify platform of device discovery */
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800547extern int (*platform_notify)(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800549extern int (*platform_notify_remove)(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551
552/**
553 * get_device - atomically increment the reference count for the device.
554 *
555 */
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800556extern struct device *get_device(struct device *dev);
557extern void put_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559
Rytchkov Alexey116f232b2006-03-22 00:58:53 +0100560/* drivers/base/power/shutdown.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561extern void device_shutdown(void);
562
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200563/* drivers/base/sys.c */
564extern void sysdev_shutdown(void);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/* debugging and troubleshooting/diagnostic helpers. */
Alan Stern3e956372006-06-16 17:10:48 -0400567extern const char *dev_driver_string(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568#define dev_printk(level, dev, format, arg...) \
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800569 printk(level "%s %s: " format , dev_driver_string(dev) , \
570 (dev)->bus_id , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Emil Medve7b8712e2007-10-30 14:37:14 -0500572#define dev_emerg(dev, format, arg...) \
573 dev_printk(KERN_EMERG , dev , format , ## arg)
574#define dev_alert(dev, format, arg...) \
575 dev_printk(KERN_ALERT , dev , format , ## arg)
576#define dev_crit(dev, format, arg...) \
577 dev_printk(KERN_CRIT , dev , format , ## arg)
578#define dev_err(dev, format, arg...) \
579 dev_printk(KERN_ERR , dev , format , ## arg)
580#define dev_warn(dev, format, arg...) \
581 dev_printk(KERN_WARNING , dev , format , ## arg)
582#define dev_notice(dev, format, arg...) \
583 dev_printk(KERN_NOTICE , dev , format , ## arg)
584#define dev_info(dev, format, arg...) \
585 dev_printk(KERN_INFO , dev , format , ## arg)
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#ifdef DEBUG
588#define dev_dbg(dev, format, arg...) \
589 dev_printk(KERN_DEBUG , dev , format , ## arg)
590#else
Dan Williams404d5b12007-04-26 00:12:10 -0700591static inline int __attribute__ ((format (printf, 2, 3)))
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800592dev_dbg(struct device *dev, const char *fmt, ...)
Dan Williams404d5b12007-04-26 00:12:10 -0700593{
594 return 0;
595}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
597
David Brownellaebdc3b2007-07-12 22:08:22 -0700598#ifdef VERBOSE_DEBUG
599#define dev_vdbg dev_dbg
600#else
601static inline int __attribute__ ((format (printf, 2, 3)))
Greg Kroah-Hartmand4629432008-01-24 21:04:46 -0800602dev_vdbg(struct device *dev, const char *fmt, ...)
David Brownellaebdc3b2007-07-12 22:08:22 -0700603{
604 return 0;
605}
606#endif
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/* Create alias, so I can be autoloaded. */
609#define MODULE_ALIAS_CHARDEV(major,minor) \
610 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
611#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
612 MODULE_ALIAS("char-major-" __stringify(major) "-*")
613#endif /* _DEVICE_H_ */