[PATCH] Add initial implementation of klist helpers.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / include / linux / device.h
CommitLineData
1da177e4
LT
1/*
2 * device.h - generic, centralized driver model
3 *
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5 *
6 * This file is released under the GPLv2
7 *
8 * See Documentation/driver-model/ for more information.
9 */
10
11#ifndef _DEVICE_H_
12#define _DEVICE_H_
13
14#include <linux/config.h>
15#include <linux/ioport.h>
16#include <linux/kobject.h>
17#include <linux/list.h>
18#include <linux/types.h>
19#include <linux/module.h>
20#include <linux/pm.h>
21#include <asm/semaphore.h>
22#include <asm/atomic.h>
23
24#define DEVICE_NAME_SIZE 50
25#define DEVICE_NAME_HALF __stringify(20) /* Less than half to accommodate slop */
26#define DEVICE_ID_SIZE 32
27#define BUS_ID_SIZE KOBJ_NAME_LEN
28
29
30enum {
31 SUSPEND_NOTIFY,
32 SUSPEND_SAVE_STATE,
33 SUSPEND_DISABLE,
34 SUSPEND_POWER_DOWN,
35};
36
37enum {
38 RESUME_POWER_ON,
39 RESUME_RESTORE_STATE,
40 RESUME_ENABLE,
41};
42
43struct device;
44struct device_driver;
45struct class;
46struct class_device;
1da177e4
LT
47
48struct bus_type {
8d790d74 49 const char * name;
1da177e4
LT
50
51 struct subsystem subsys;
52 struct kset drivers;
53 struct kset devices;
54
55 struct bus_attribute * bus_attrs;
56 struct device_attribute * dev_attrs;
57 struct driver_attribute * drv_attrs;
58
59 int (*match)(struct device * dev, struct device_driver * drv);
60 int (*hotplug) (struct device *dev, char **envp,
61 int num_envp, char *buffer, int buffer_size);
62 int (*suspend)(struct device * dev, pm_message_t state);
63 int (*resume)(struct device * dev);
64};
65
66extern int bus_register(struct bus_type * bus);
67extern void bus_unregister(struct bus_type * bus);
68
69extern int bus_rescan_devices(struct bus_type * bus);
70
71extern struct bus_type * get_bus(struct bus_type * bus);
72extern void put_bus(struct bus_type * bus);
73
74extern struct bus_type * find_bus(char * name);
75
76/* iterator helpers for buses */
77
78int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
79 int (*fn)(struct device *, void *));
80
81int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
82 void * data, int (*fn)(struct device_driver *, void *));
83
84
85/* driverfs interface for exporting bus attributes */
86
87struct bus_attribute {
88 struct attribute attr;
89 ssize_t (*show)(struct bus_type *, char * buf);
90 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
91};
92
93#define BUS_ATTR(_name,_mode,_show,_store) \
94struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
95
96extern int bus_create_file(struct bus_type *, struct bus_attribute *);
97extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
98
99struct device_driver {
8d790d74 100 const char * name;
1da177e4
LT
101 struct bus_type * bus;
102
103 struct completion unloaded;
104 struct kobject kobj;
105 struct list_head devices;
106
8d790d74 107 struct module * owner;
1da177e4
LT
108
109 int (*probe) (struct device * dev);
8d790d74 110 int (*remove) (struct device * dev);
1da177e4
LT
111 void (*shutdown) (struct device * dev);
112 int (*suspend) (struct device * dev, pm_message_t state, u32 level);
113 int (*resume) (struct device * dev, u32 level);
114};
115
116
117extern int driver_register(struct device_driver * drv);
118extern void driver_unregister(struct device_driver * drv);
119
120extern struct device_driver * get_driver(struct device_driver * drv);
121extern void put_driver(struct device_driver * drv);
122extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
123
124
125/* driverfs interface for exporting driver attributes */
126
127struct driver_attribute {
128 struct attribute attr;
129 ssize_t (*show)(struct device_driver *, char * buf);
130 ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
131};
132
133#define DRIVER_ATTR(_name,_mode,_show,_store) \
134struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
135
136extern int driver_create_file(struct device_driver *, struct driver_attribute *);
137extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
138
fae3cd00
PM
139extern int driver_for_each_device(struct device_driver * drv, struct device * start,
140 void * data, int (*fn)(struct device *, void *));
141
1da177e4
LT
142
143/*
144 * device classes
145 */
146struct class {
8d790d74 147 const char * name;
e9ba6365 148 struct module * owner;
1da177e4
LT
149
150 struct subsystem subsys;
151 struct list_head children;
152 struct list_head interfaces;
153 struct semaphore sem; /* locks both the children and interfaces lists */
154
155 struct class_attribute * class_attrs;
156 struct class_device_attribute * class_dev_attrs;
157
158 int (*hotplug)(struct class_device *dev, char **envp,
159 int num_envp, char *buffer, int buffer_size);
160
161 void (*release)(struct class_device *dev);
162 void (*class_release)(struct class *class);
163};
164
165extern int class_register(struct class *);
166extern void class_unregister(struct class *);
167
168extern struct class * class_get(struct class *);
169extern void class_put(struct class *);
170
171
172struct class_attribute {
173 struct attribute attr;
174 ssize_t (*show)(struct class *, char * buf);
175 ssize_t (*store)(struct class *, const char * buf, size_t count);
176};
177
178#define CLASS_ATTR(_name,_mode,_show,_store) \
179struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
180
181extern int class_create_file(struct class *, const struct class_attribute *);
182extern void class_remove_file(struct class *, const struct class_attribute *);
183
184
185struct class_device {
186 struct list_head node;
187
188 struct kobject kobj;
189 struct class * class; /* required */
190 dev_t devt; /* dev_t, creates the sysfs "dev" */
e9ba6365 191 struct class_device_attribute *devt_attr;
1da177e4
LT
192 struct device * dev; /* not necessary, but nice to have */
193 void * class_data; /* class-specific data */
194
195 char class_id[BUS_ID_SIZE]; /* unique to this class */
196};
197
198static inline void *
199class_get_devdata (struct class_device *dev)
200{
201 return dev->class_data;
202}
203
204static inline void
205class_set_devdata (struct class_device *dev, void *data)
206{
207 dev->class_data = data;
208}
209
210
211extern int class_device_register(struct class_device *);
212extern void class_device_unregister(struct class_device *);
213extern void class_device_initialize(struct class_device *);
214extern int class_device_add(struct class_device *);
215extern void class_device_del(struct class_device *);
216
217extern int class_device_rename(struct class_device *, char *);
218
219extern struct class_device * class_device_get(struct class_device *);
220extern void class_device_put(struct class_device *);
221
222struct class_device_attribute {
223 struct attribute attr;
224 ssize_t (*show)(struct class_device *, char * buf);
225 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
226};
227
228#define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
229struct class_device_attribute class_device_attr_##_name = \
230 __ATTR(_name,_mode,_show,_store)
231
232extern int class_device_create_file(struct class_device *,
233 const struct class_device_attribute *);
234extern void class_device_remove_file(struct class_device *,
235 const struct class_device_attribute *);
236extern int class_device_create_bin_file(struct class_device *,
237 struct bin_attribute *);
238extern void class_device_remove_bin_file(struct class_device *,
239 struct bin_attribute *);
240
241struct class_interface {
242 struct list_head node;
243 struct class *class;
244
245 int (*add) (struct class_device *);
246 void (*remove) (struct class_device *);
247};
248
249extern int class_interface_register(struct class_interface *);
250extern void class_interface_unregister(struct class_interface *);
251
e9ba6365
GKH
252extern struct class *class_create(struct module *owner, char *name);
253extern void class_destroy(struct class *cls);
254extern struct class_device *class_device_create(struct class *cls, dev_t devt,
255 struct device *device, char *fmt, ...)
256 __attribute__((format(printf,4,5)));
257extern void class_device_destroy(struct class *cls, dev_t devt);
258
1da177e4
LT
259
260struct device {
261 struct list_head node; /* node in sibling list */
262 struct list_head bus_list; /* node in bus's list */
263 struct list_head driver_list;
264 struct list_head children;
265 struct device * parent;
266
267 struct kobject kobj;
268 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
269
af70316a
PM
270 struct semaphore sem; /* semaphore to synchronize calls to
271 * its driver.
272 */
273
1da177e4
LT
274 struct bus_type * bus; /* type of bus device is on */
275 struct device_driver *driver; /* which driver has allocated this
276 device */
277 void *driver_data; /* data private to the driver */
278 void *platform_data; /* Platform specific data (e.g. ACPI,
279 BIOS data relevant to device) */
280 struct dev_pm_info power;
281
1da177e4
LT
282 u64 *dma_mask; /* dma mask (if dma'able device) */
283 u64 coherent_dma_mask;/* Like dma_mask, but for
284 alloc_coherent mappings as
285 not all hardware supports
286 64 bit addresses for consistent
287 allocations such descriptors. */
288
289 struct list_head dma_pools; /* dma pools (if dma'ble) */
290
291 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
292 override */
293
294 void (*release)(struct device * dev);
295};
296
297static inline struct device *
298list_to_dev(struct list_head *node)
299{
300 return list_entry(node, struct device, node);
301}
302
303static inline void *
304dev_get_drvdata (struct device *dev)
305{
306 return dev->driver_data;
307}
308
309static inline void
310dev_set_drvdata (struct device *dev, void *data)
311{
312 dev->driver_data = data;
313}
314
315/*
316 * High level routines for use by the bus drivers
317 */
318extern int device_register(struct device * dev);
319extern void device_unregister(struct device * dev);
320extern void device_initialize(struct device * dev);
321extern int device_add(struct device * dev);
322extern void device_del(struct device * dev);
323extern int device_for_each_child(struct device *, void *,
324 int (*fn)(struct device *, void *));
325
326/*
327 * Manual binding of a device to driver. See drivers/base/bus.c
328 * for information on use.
329 */
330extern int driver_probe_device(struct device_driver * drv, struct device * dev);
331extern void device_bind_driver(struct device * dev);
332extern void device_release_driver(struct device * dev);
333extern int device_attach(struct device * dev);
334extern void driver_attach(struct device_driver * drv);
335
336
337/* driverfs interface for exporting device attributes */
338
339struct device_attribute {
340 struct attribute attr;
341 ssize_t (*show)(struct device * dev, char * buf);
342 ssize_t (*store)(struct device * dev, const char * buf, size_t count);
343};
344
345#define DEVICE_ATTR(_name,_mode,_show,_store) \
346struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
347
348
349extern int device_create_file(struct device *device, struct device_attribute * entry);
350extern void device_remove_file(struct device * dev, struct device_attribute * attr);
351
352/*
353 * Platform "fixup" functions - allow the platform to have their say
354 * about devices and actions that the general device layer doesn't
355 * know about.
356 */
357/* Notify platform of device discovery */
358extern int (*platform_notify)(struct device * dev);
359
360extern int (*platform_notify_remove)(struct device * dev);
361
362
363/**
364 * get_device - atomically increment the reference count for the device.
365 *
366 */
367extern struct device * get_device(struct device * dev);
368extern void put_device(struct device * dev);
369extern struct device *device_find(const char *name, struct bus_type *bus);
370
371
372/* drivers/base/platform.c */
373
374struct platform_device {
8d790d74 375 const char * name;
1da177e4
LT
376 u32 id;
377 struct device dev;
378 u32 num_resources;
379 struct resource * resource;
380};
381
382#define to_platform_device(x) container_of((x), struct platform_device, dev)
383
384extern int platform_device_register(struct platform_device *);
385extern void platform_device_unregister(struct platform_device *);
386
387extern struct bus_type platform_bus_type;
388extern struct device platform_bus;
389
390extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
391extern int platform_get_irq(struct platform_device *, unsigned int);
392extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
393extern int platform_get_irq_byname(struct platform_device *, char *);
394extern int platform_add_devices(struct platform_device **, int);
395
396extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
397
398/* drivers/base/power.c */
399extern void device_shutdown(void);
400
401
402/* drivers/base/firmware.c */
403extern int firmware_register(struct subsystem *);
404extern void firmware_unregister(struct subsystem *);
405
406/* debugging and troubleshooting/diagnostic helpers. */
407#define dev_printk(level, dev, format, arg...) \
408 printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
409
410#ifdef DEBUG
411#define dev_dbg(dev, format, arg...) \
412 dev_printk(KERN_DEBUG , dev , format , ## arg)
413#else
414#define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
415#endif
416
417#define dev_err(dev, format, arg...) \
418 dev_printk(KERN_ERR , dev , format , ## arg)
419#define dev_info(dev, format, arg...) \
420 dev_printk(KERN_INFO , dev , format , ## arg)
421#define dev_warn(dev, format, arg...) \
422 dev_printk(KERN_WARNING , dev , format , ## arg)
423
424/* Create alias, so I can be autoloaded. */
425#define MODULE_ALIAS_CHARDEV(major,minor) \
426 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
427#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
428 MODULE_ALIAS("char-major-" __stringify(major) "-*")
429#endif /* _DEVICE_H_ */