Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * device.h - generic, centralized driver model
3 *
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5 * Copyright (c) 2004-2007 Greg Kroah-Hartman <gregkh@suse.de>
6 *
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
15#include <linux/ioport.h>
16#include <linux/kobject.h>
17#include <linux/klist.h>
18#include <linux/list.h>
19#include <linux/compiler.h>
20#include <linux/types.h>
21#include <linux/module.h>
22#include <linux/pm.h>
23#include <asm/semaphore.h>
24#include <asm/atomic.h>
25#include <asm/device.h>
26
27#define DEVICE_NAME_SIZE 50
28/* DEVICE_NAME_HALF is really less than half to accommodate slop */
29#define DEVICE_NAME_HALF __stringify(20)
30#define DEVICE_ID_SIZE 32
31#define BUS_ID_SIZE KOBJ_NAME_LEN
32
33
34struct device;
35struct device_driver;
36struct driver_private;
37struct class;
38struct class_device;
39struct bus_type;
40struct bus_type_private;
41
42struct bus_attribute {
43 struct attribute attr;
44 ssize_t (*show)(struct bus_type *bus, char *buf);
45 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
46};
47
48#define BUS_ATTR(_name, _mode, _show, _store) \
49struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
50
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 *);
54
55struct bus_type {
56 const char *name;
57 struct bus_attribute *bus_attrs;
58 struct device_attribute *dev_attrs;
59 struct driver_attribute *drv_attrs;
60
61 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);
66
67 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);
71
72 struct bus_type_private *p;
73};
74
75extern int __must_check bus_register(struct bus_type *bus);
76extern void bus_unregister(struct bus_type *bus);
77
78extern int __must_check bus_rescan_devices(struct bus_type *bus);
79
80/* iterator helpers for buses */
81
82int 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));
87struct device *bus_find_device_by_name(struct bus_type *bus,
88 struct device *start,
89 const char *name);
90
91int __must_check bus_for_each_drv(struct bus_type *bus,
92 struct device_driver *start, void *data,
93 int (*fn)(struct device_driver *, void *));
94
95/*
96 * Bus notifiers: Get notified of addition/removal of devices
97 * and binding/unbinding of drivers to devices.
98 * In the long run, it should be a replacement for the platform
99 * notify hooks.
100 */
101struct notifier_block;
102
103extern int bus_register_notifier(struct bus_type *bus,
104 struct notifier_block *nb);
105extern int bus_unregister_notifier(struct bus_type *bus,
106 struct notifier_block *nb);
107
108/* All 4 notifers below get called with the target struct device *
109 * as an argument. Note that those functions are likely to be called
110 * with the device semaphore held in the core, so be careful.
111 */
112#define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
113#define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
114#define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
115#define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
116 unbound */
117
118extern struct kset *bus_get_kset(struct bus_type *bus);
119extern struct klist *bus_get_device_klist(struct bus_type *bus);
120
121struct device_driver {
122 const char *name;
123 struct bus_type *bus;
124
125 struct module *owner;
126 const char *mod_name; /* used for built-in modules */
127
128 int (*probe) (struct device *dev);
129 int (*remove) (struct device *dev);
130 void (*shutdown) (struct device *dev);
131 int (*suspend) (struct device *dev, pm_message_t state);
132 int (*resume) (struct device *dev);
133 struct attribute_group **groups;
134
135 struct driver_private *p;
136};
137
138
139extern int __must_check driver_register(struct device_driver *drv);
140extern void driver_unregister(struct device_driver *drv);
141
142extern struct device_driver *get_driver(struct device_driver *drv);
143extern void put_driver(struct device_driver *drv);
144extern struct device_driver *driver_find(const char *name,
145 struct bus_type *bus);
146extern int driver_probe_done(void);
147
148/* sysfs interface for exporting driver attributes */
149
150struct driver_attribute {
151 struct attribute attr;
152 ssize_t (*show)(struct device_driver *driver, char *buf);
153 ssize_t (*store)(struct device_driver *driver, const char *buf,
154 size_t count);
155};
156
157#define DRIVER_ATTR(_name, _mode, _show, _store) \
158struct driver_attribute driver_attr_##_name = \
159 __ATTR(_name, _mode, _show, _store)
160
161extern int __must_check driver_create_file(struct device_driver *driver,
162 struct driver_attribute *attr);
163extern void driver_remove_file(struct device_driver *driver,
164 struct driver_attribute *attr);
165
166extern int __must_check driver_add_kobj(struct device_driver *drv,
167 struct kobject *kobj,
168 const char *fmt, ...);
169
170extern int __must_check driver_for_each_device(struct device_driver *drv,
171 struct device *start,
172 void *data,
173 int (*fn)(struct device *dev,
174 void *));
175struct device *driver_find_device(struct device_driver *drv,
176 struct device *start, void *data,
177 int (*match)(struct device *dev, void *data));
178
179/*
180 * device classes
181 */
182struct class {
183 const char *name;
184 struct module *owner;
185
186 struct kset subsys;
187 struct list_head children;
188 struct list_head devices;
189 struct list_head interfaces;
190 struct kset class_dirs;
191 struct semaphore sem; /* locks children, devices, interfaces */
192 struct class_attribute *class_attrs;
193 struct class_device_attribute *class_dev_attrs;
194 struct device_attribute *dev_attrs;
195
196 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
197 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
198
199 void (*release)(struct class_device *dev);
200 void (*class_release)(struct class *class);
201 void (*dev_release)(struct device *dev);
202
203 int (*suspend)(struct device *dev, pm_message_t state);
204 int (*resume)(struct device *dev);
205};
206
207extern int __must_check class_register(struct class *class);
208extern void class_unregister(struct class *class);
209extern int class_for_each_device(struct class *class, void *data,
210 int (*fn)(struct device *dev, void *data));
211extern struct device *class_find_device(struct class *class, void *data,
212 int (*match)(struct device *, void *));
213extern struct class_device *class_find_child(struct class *class, void *data,
214 int (*match)(struct class_device *, void *));
215
216
217struct class_attribute {
218 struct attribute attr;
219 ssize_t (*show)(struct class *class, char *buf);
220 ssize_t (*store)(struct class *class, const char *buf, size_t count);
221};
222
223#define CLASS_ATTR(_name, _mode, _show, _store) \
224struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
225
226extern int __must_check class_create_file(struct class *class,
227 const struct class_attribute *attr);
228extern void class_remove_file(struct class *class,
229 const struct class_attribute *attr);
230
231struct class_device_attribute {
232 struct attribute attr;
233 ssize_t (*show)(struct class_device *, char *buf);
234 ssize_t (*store)(struct class_device *, const char *buf, size_t count);
235};
236
237#define CLASS_DEVICE_ATTR(_name, _mode, _show, _store) \
238struct class_device_attribute class_device_attr_##_name = \
239 __ATTR(_name, _mode, _show, _store)
240
241extern int __must_check class_device_create_file(struct class_device *,
242 const struct class_device_attribute *);
243
244/**
245 * struct class_device - class devices
246 * @class: pointer to the parent class for this class device. This is required.
247 * @devt: for internal use by the driver core only.
248 * @node: for internal use by the driver core only.
249 * @kobj: for internal use by the driver core only.
250 * @groups: optional additional groups to be created
251 * @dev: if set, a symlink to the struct device is created in the sysfs
252 * directory for this struct class device.
253 * @class_data: pointer to whatever you want to store here for this struct
254 * class_device. Use class_get_devdata() and class_set_devdata() to get and
255 * set this pointer.
256 * @parent: pointer to a struct class_device that is the parent of this struct
257 * class_device. If NULL, this class_device will show up at the root of the
258 * struct class in sysfs (which is probably what you want to have happen.)
259 * @release: pointer to a release function for this struct class_device. If
260 * set, this will be called instead of the class specific release function.
261 * Only use this if you want to override the default release function, like
262 * when you are nesting class_device structures.
263 * @uevent: pointer to a uevent function for this struct class_device. If
264 * set, this will be called instead of the class specific uevent function.
265 * Only use this if you want to override the default uevent function, like
266 * when you are nesting class_device structures.
267 */
268struct class_device {
269 struct list_head node;
270
271 struct kobject kobj;
272 struct class *class;
273 dev_t devt;
274 struct device *dev;
275 void *class_data;
276 struct class_device *parent;
277 struct attribute_group **groups;
278
279 void (*release)(struct class_device *dev);
280 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
281 char class_id[BUS_ID_SIZE];
282};
283
284static inline void *class_get_devdata(struct class_device *dev)
285{
286 return dev->class_data;
287}
288
289static inline void class_set_devdata(struct class_device *dev, void *data)
290{
291 dev->class_data = data;
292}
293
294
295extern int __must_check class_device_register(struct class_device *);
296extern void class_device_unregister(struct class_device *);
297extern void class_device_initialize(struct class_device *);
298extern int __must_check class_device_add(struct class_device *);
299extern void class_device_del(struct class_device *);
300
301extern struct class_device *class_device_get(struct class_device *);
302extern void class_device_put(struct class_device *);
303
304extern void class_device_remove_file(struct class_device *,
305 const struct class_device_attribute *);
306extern int __must_check class_device_create_bin_file(struct class_device *,
307 struct bin_attribute *);
308extern void class_device_remove_bin_file(struct class_device *,
309 struct bin_attribute *);
310
311struct class_interface {
312 struct list_head node;
313 struct class *class;
314
315 int (*add) (struct class_device *, struct class_interface *);
316 void (*remove) (struct class_device *, struct class_interface *);
317 int (*add_dev) (struct device *, struct class_interface *);
318 void (*remove_dev) (struct device *, struct class_interface *);
319};
320
321extern int __must_check class_interface_register(struct class_interface *);
322extern void class_interface_unregister(struct class_interface *);
323
324extern struct class *class_create(struct module *owner, const char *name);
325extern void class_destroy(struct class *cls);
326extern struct class_device *class_device_create(struct class *cls,
327 struct class_device *parent,
328 dev_t devt,
329 struct device *device,
330 const char *fmt, ...)
331 __attribute__((format(printf, 5, 6)));
332extern void class_device_destroy(struct class *cls, dev_t devt);
333
334/*
335 * The type of device, "struct device" is embedded in. A class
336 * or bus can contain devices of different types
337 * like "partitions" and "disks", "mouse" and "event".
338 * This identifies the device type and carries type-specific
339 * information, equivalent to the kobj_type of a kobject.
340 * If "name" is specified, the uevent will contain it in
341 * the DEVTYPE variable.
342 */
343struct device_type {
344 const char *name;
345 struct attribute_group **groups;
346 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
347 void (*release)(struct device *dev);
348 int (*suspend)(struct device *dev, pm_message_t state);
349 int (*resume)(struct device *dev);
350};
351
352/* interface for exporting device attributes */
353struct device_attribute {
354 struct attribute attr;
355 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
356 char *buf);
357 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
358 const char *buf, size_t count);
359};
360
361#define DEVICE_ATTR(_name, _mode, _show, _store) \
362struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
363
364extern int __must_check device_create_file(struct device *device,
365 struct device_attribute *entry);
366extern void device_remove_file(struct device *dev,
367 struct device_attribute *attr);
368extern int __must_check device_create_bin_file(struct device *dev,
369 struct bin_attribute *attr);
370extern void device_remove_bin_file(struct device *dev,
371 struct bin_attribute *attr);
372extern int device_schedule_callback_owner(struct device *dev,
373 void (*func)(struct device *dev), struct module *owner);
374
375/* This is a macro to avoid include problems with THIS_MODULE */
376#define device_schedule_callback(dev, func) \
377 device_schedule_callback_owner(dev, func, THIS_MODULE)
378
379/* device resource management */
380typedef void (*dr_release_t)(struct device *dev, void *res);
381typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
382
383#ifdef CONFIG_DEBUG_DEVRES
384extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
385 const char *name);
386#define devres_alloc(release, size, gfp) \
387 __devres_alloc(release, size, gfp, #release)
388#else
389extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
390#endif
391extern void devres_free(void *res);
392extern void devres_add(struct device *dev, void *res);
393extern void *devres_find(struct device *dev, dr_release_t release,
394 dr_match_t match, void *match_data);
395extern void *devres_get(struct device *dev, void *new_res,
396 dr_match_t match, void *match_data);
397extern void *devres_remove(struct device *dev, dr_release_t release,
398 dr_match_t match, void *match_data);
399extern int devres_destroy(struct device *dev, dr_release_t release,
400 dr_match_t match, void *match_data);
401
402/* devres group */
403extern void * __must_check devres_open_group(struct device *dev, void *id,
404 gfp_t gfp);
405extern void devres_close_group(struct device *dev, void *id);
406extern void devres_remove_group(struct device *dev, void *id);
407extern int devres_release_group(struct device *dev, void *id);
408
409/* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
410extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
411extern void devm_kfree(struct device *dev, void *p);
412
413struct device_dma_parameters {
414 /*
415 * a low level driver may set these to teach IOMMU code about
416 * sg limitations.
417 */
418 unsigned int max_segment_size;
419 unsigned long segment_boundary_mask;
420};
421
422struct device {
423 struct klist klist_children;
424 struct klist_node knode_parent; /* node in sibling list */
425 struct klist_node knode_driver;
426 struct klist_node knode_bus;
427 struct device *parent;
428
429 struct kobject kobj;
430 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
431 struct device_type *type;
432 unsigned is_registered:1;
433 unsigned uevent_suppress:1;
434
435 struct semaphore sem; /* semaphore to synchronize calls to
436 * its driver.
437 */
438
439 struct bus_type *bus; /* type of bus device is on */
440 struct device_driver *driver; /* which driver has allocated this
441 device */
442 void *driver_data; /* data private to the driver */
443 void *platform_data; /* Platform specific data, device
444 core doesn't touch it */
445 struct dev_pm_info power;
446
447#ifdef CONFIG_NUMA
448 int numa_node; /* NUMA node this device is close to */
449#endif
450 u64 *dma_mask; /* dma mask (if dma'able device) */
451 u64 coherent_dma_mask;/* Like dma_mask, but for
452 alloc_coherent mappings as
453 not all hardware supports
454 64 bit addresses for consistent
455 allocations such descriptors. */
456
457 struct device_dma_parameters *dma_parms;
458
459 struct list_head dma_pools; /* dma pools (if dma'ble) */
460
461 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
462 override */
463 /* arch specific additions */
464 struct dev_archdata archdata;
465
466 spinlock_t devres_lock;
467 struct list_head devres_head;
468
469 /* class_device migration path */
470 struct list_head node;
471 struct class *class;
472 dev_t devt; /* dev_t, creates the sysfs "dev" */
473 struct attribute_group **groups; /* optional groups */
474
475 void (*release)(struct device *dev);
476};
477
478#ifdef CONFIG_NUMA
479static inline int dev_to_node(struct device *dev)
480{
481 return dev->numa_node;
482}
483static inline void set_dev_node(struct device *dev, int node)
484{
485 dev->numa_node = node;
486}
487#else
488static inline int dev_to_node(struct device *dev)
489{
490 return -1;
491}
492static inline void set_dev_node(struct device *dev, int node)
493{
494}
495#endif
496
497static inline void *dev_get_drvdata(struct device *dev)
498{
499 return dev->driver_data;
500}
501
502static inline void dev_set_drvdata(struct device *dev, void *data)
503{
504 dev->driver_data = data;
505}
506
507static inline int device_is_registered(struct device *dev)
508{
509 return dev->is_registered;
510}
511
512void driver_init(void);
513
514/*
515 * High level routines for use by the bus drivers
516 */
517extern int __must_check device_register(struct device *dev);
518extern void device_unregister(struct device *dev);
519extern void device_initialize(struct device *dev);
520extern int __must_check device_add(struct device *dev);
521extern void device_del(struct device *dev);
522extern int device_for_each_child(struct device *dev, void *data,
523 int (*fn)(struct device *dev, void *data));
524extern struct device *device_find_child(struct device *dev, void *data,
525 int (*match)(struct device *dev, void *data));
526extern int device_rename(struct device *dev, char *new_name);
527extern int device_move(struct device *dev, struct device *new_parent);
528
529/*
530 * Manual binding of a device to driver. See drivers/base/bus.c
531 * for information on use.
532 */
533extern int __must_check device_bind_driver(struct device *dev);
534extern void device_release_driver(struct device *dev);
535extern int __must_check device_attach(struct device *dev);
536extern int __must_check driver_attach(struct device_driver *drv);
537extern int __must_check device_reprobe(struct device *dev);
538
539/*
540 * Easy functions for dynamically creating devices on the fly
541 */
542extern struct device *device_create(struct class *cls, struct device *parent,
543 dev_t devt, const char *fmt, ...)
544 __attribute__((format(printf, 4, 5)));
545extern void device_destroy(struct class *cls, dev_t devt);
546#ifdef CONFIG_PM_SLEEP
547extern void destroy_suspended_device(struct class *cls, dev_t devt);
548extern void device_pm_schedule_removal(struct device *);
549#else /* !CONFIG_PM_SLEEP */
550static inline void destroy_suspended_device(struct class *cls, dev_t devt)
551{
552 device_destroy(cls, devt);
553}
554
555static inline void device_pm_schedule_removal(struct device *dev)
556{
557 device_unregister(dev);
558}
559#endif /* !CONFIG_PM_SLEEP */
560
561/*
562 * Platform "fixup" functions - allow the platform to have their say
563 * about devices and actions that the general device layer doesn't
564 * know about.
565 */
566/* Notify platform of device discovery */
567extern int (*platform_notify)(struct device *dev);
568
569extern int (*platform_notify_remove)(struct device *dev);
570
571
572/**
573 * get_device - atomically increment the reference count for the device.
574 *
575 */
576extern struct device *get_device(struct device *dev);
577extern void put_device(struct device *dev);
578
579
580/* drivers/base/power/shutdown.c */
581extern void device_shutdown(void);
582
583/* drivers/base/sys.c */
584extern void sysdev_shutdown(void);
585
586/* debugging and troubleshooting/diagnostic helpers. */
587extern const char *dev_driver_string(struct device *dev);
588#define dev_printk(level, dev, format, arg...) \
589 printk(level "%s %s: " format , dev_driver_string(dev) , \
590 (dev)->bus_id , ## arg)
591
592#define dev_emerg(dev, format, arg...) \
593 dev_printk(KERN_EMERG , dev , format , ## arg)
594#define dev_alert(dev, format, arg...) \
595 dev_printk(KERN_ALERT , dev , format , ## arg)
596#define dev_crit(dev, format, arg...) \
597 dev_printk(KERN_CRIT , dev , format , ## arg)
598#define dev_err(dev, format, arg...) \
599 dev_printk(KERN_ERR , dev , format , ## arg)
600#define dev_warn(dev, format, arg...) \
601 dev_printk(KERN_WARNING , dev , format , ## arg)
602#define dev_notice(dev, format, arg...) \
603 dev_printk(KERN_NOTICE , dev , format , ## arg)
604#define dev_info(dev, format, arg...) \
605 dev_printk(KERN_INFO , dev , format , ## arg)
606
607#ifdef DEBUG
608#define dev_dbg(dev, format, arg...) \
609 dev_printk(KERN_DEBUG , dev , format , ## arg)
610#else
611static inline int __attribute__ ((format (printf, 2, 3)))
612dev_dbg(struct device *dev, const char *fmt, ...)
613{
614 return 0;
615}
616#endif
617
618#ifdef VERBOSE_DEBUG
619#define dev_vdbg dev_dbg
620#else
621static inline int __attribute__ ((format (printf, 2, 3)))
622dev_vdbg(struct device *dev, const char *fmt, ...)
623{
624 return 0;
625}
626#endif
627
628/* Create alias, so I can be autoloaded. */
629#define MODULE_ALIAS_CHARDEV(major,minor) \
630 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
631#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
632 MODULE_ALIAS("char-major-" __stringify(major) "-*")
633#endif /* _DEVICE_H_ */