1
2
3
4
5
6
7
8
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/spinlock.h>
19#include <linux/types.h>
20#include <linux/module.h>
21#include <linux/pm.h>
22#include <asm/semaphore.h>
23#include <asm/atomic.h>
24
25#define DEVICE_NAME_SIZE 50
26#define DEVICE_NAME_HALF __stringify(20)
27#define DEVICE_ID_SIZE 32
28#define BUS_ID_SIZE KOBJ_NAME_LEN
29
30
31enum {
32 SUSPEND_NOTIFY,
33 SUSPEND_SAVE_STATE,
34 SUSPEND_DISABLE,
35 SUSPEND_POWER_DOWN,
36};
37
38enum {
39 RESUME_POWER_ON,
40 RESUME_RESTORE_STATE,
41 RESUME_ENABLE,
42};
43
44struct device;
45struct device_driver;
46struct class;
47struct class_device;
48struct class_simple;
49
50struct bus_type {
51 char * name;
52
53 struct subsystem subsys;
54 struct kset drivers;
55 struct kset devices;
56
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 struct device * (*add) (struct device * parent, char * bus_id);
63 int (*hotplug) (struct device *dev, char **envp,
64 int num_envp, char *buffer, int buffer_size);
65 int (*suspend)(struct device * dev, u32 state);
66 int (*resume)(struct device * dev);
67};
68
69extern int bus_register(struct bus_type * bus);
70extern void bus_unregister(struct bus_type * bus);
71
72extern int bus_rescan_devices(struct bus_type * bus);
73
74extern struct bus_type * get_bus(struct bus_type * bus);
75extern void put_bus(struct bus_type * bus);
76
77extern struct bus_type * find_bus(char * name);
78
79
80
81int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
82 int (*fn)(struct device *, void *));
83
84int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
85 void * data, int (*fn)(struct device_driver *, void *));
86
87
88
89
90struct bus_attribute {
91 struct attribute attr;
92 ssize_t (*show)(struct bus_type *, char * buf);
93 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
94};
95
96#define BUS_ATTR(_name,_mode,_show,_store) \
97struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
98
99extern int bus_create_file(struct bus_type *, struct bus_attribute *);
100extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
101
102struct device_driver {
103 char * name;
104 struct bus_type * bus;
105
106 struct semaphore unload_sem;
107 struct kobject kobj;
108 struct list_head devices;
109
110 int (*probe) (struct device * dev);
111 int (*remove) (struct device * dev);
112 void (*shutdown) (struct device * dev);
113 int (*suspend) (struct device * dev, u32 state, u32 level);
114 int (*resume) (struct device * dev, u32 level);
115};
116
117
118extern int driver_register(struct device_driver * drv);
119extern void driver_unregister(struct device_driver * drv);
120
121extern struct device_driver * get_driver(struct device_driver * drv);
122extern void put_driver(struct device_driver * drv);
123extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
124
125
126
127
128struct driver_attribute {
129 struct attribute attr;
130 ssize_t (*show)(struct device_driver *, char * buf);
131 ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
132};
133
134#define DRIVER_ATTR(_name,_mode,_show,_store) \
135struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
136
137extern int driver_create_file(struct device_driver *, struct driver_attribute *);
138extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
139
140
141
142
143
144struct class {
145 char * name;
146
147 struct subsystem subsys;
148 struct list_head children;
149 struct list_head interfaces;
150
151 struct class_attribute * class_attrs;
152 struct class_device_attribute * class_dev_attrs;
153
154 int (*hotplug)(struct class_device *dev, char **envp,
155 int num_envp, char *buffer, int buffer_size);
156
157 void (*release)(struct class_device *dev);
158 void (*class_release)(struct class *class);
159};
160
161extern int class_register(struct class *);
162extern void class_unregister(struct class *);
163
164extern struct class * class_get(struct class *);
165extern void class_put(struct class *);
166
167
168struct class_attribute {
169 struct attribute attr;
170 ssize_t (*show)(struct class *, char * buf);
171 ssize_t (*store)(struct class *, const char * buf, size_t count);
172};
173
174#define CLASS_ATTR(_name,_mode,_show,_store) \
175struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
176
177extern int class_create_file(struct class *, const struct class_attribute *);
178extern void class_remove_file(struct class *, const struct class_attribute *);
179
180
181struct class_device {
182 struct list_head node;
183
184 struct kobject kobj;
185 struct class * class;
186 struct device * dev;
187 void * class_data;
188
189 char class_id[BUS_ID_SIZE];
190};
191
192static inline void *
193class_get_devdata (struct class_device *dev)
194{
195 return dev->class_data;
196}
197
198static inline void
199class_set_devdata (struct class_device *dev, void *data)
200{
201 dev->class_data = data;
202}
203
204
205extern int class_device_register(struct class_device *);
206extern void class_device_unregister(struct class_device *);
207extern void class_device_initialize(struct class_device *);
208extern int class_device_add(struct class_device *);
209extern void class_device_del(struct class_device *);
210
211extern int class_device_rename(struct class_device *, char *);
212
213extern struct class_device * class_device_get(struct class_device *);
214extern void class_device_put(struct class_device *);
215
216struct class_device_attribute {
217 struct attribute attr;
218 ssize_t (*show)(struct class_device *, char * buf);
219 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
220};
221
222#define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
223struct class_device_attribute class_device_attr_##_name = \
224 __ATTR(_name,_mode,_show,_store)
225
226extern int class_device_create_file(struct class_device *,
227 const struct class_device_attribute *);
228extern void class_device_remove_file(struct class_device *,
229 const struct class_device_attribute *);
230
231
232struct class_interface {
233 struct list_head node;
234 struct class *class;
235
236 int (*add) (struct class_device *);
237 void (*remove) (struct class_device *);
238};
239
240extern int class_interface_register(struct class_interface *);
241extern void class_interface_unregister(struct class_interface *);
242
243
244extern struct class_simple *class_simple_create(struct module *owner, char *name);
245extern void class_simple_destroy(struct class_simple *cs);
246extern struct class_device *class_simple_device_add(struct class_simple *cs, dev_t dev, struct device *device, const char *fmt, ...)
247 __attribute__((format(printf,4,5)));
248extern int class_simple_set_hotplug(struct class_simple *,
249 int (*hotplug)(struct class_device *dev, char **envp, int num_envp, char *buffer, int buffer_size));
250extern void class_simple_device_remove(dev_t dev);
251
252
253struct device {
254 struct list_head node;
255 struct list_head bus_list;
256 struct list_head driver_list;
257 struct list_head children;
258 struct device * parent;
259
260 struct kobject kobj;
261 char bus_id[BUS_ID_SIZE];
262
263 struct bus_type * bus;
264 struct device_driver *driver;
265
266 void *driver_data;
267 void *platform_data;
268
269 struct dev_pm_info power;
270 u32 power_state;
271
272
273
274
275 unsigned char *saved_state;
276 u32 detach_state;
277
278
279 u64 *dma_mask;
280 u64 coherent_dma_mask;
281
282
283
284
285
286 struct list_head dma_pools;
287
288 void (*release)(struct device * dev);
289};
290
291static inline struct device *
292list_to_dev(struct list_head *node)
293{
294 return list_entry(node, struct device, node);
295}
296
297static inline void *
298dev_get_drvdata (struct device *dev)
299{
300 return dev->driver_data;
301}
302
303static inline void
304dev_set_drvdata (struct device *dev, void *data)
305{
306 dev->driver_data = data;
307}
308
309
310
311
312extern int device_register(struct device * dev);
313extern void device_unregister(struct device * dev);
314extern void device_initialize(struct device * dev);
315extern int device_add(struct device * dev);
316extern void device_del(struct device * dev);
317extern int device_for_each_child(struct device *, void *,
318 int (*fn)(struct device *, void *));
319
320
321
322
323
324extern void device_bind_driver(struct device * dev);
325extern void device_release_driver(struct device * dev);
326extern void driver_attach(struct device_driver * drv);
327
328
329
330
331struct device_attribute {
332 struct attribute attr;
333 ssize_t (*show)(struct device * dev, char * buf);
334 ssize_t (*store)(struct device * dev, const char * buf, size_t count);
335};
336
337#define DEVICE_ATTR(_name,_mode,_show,_store) \
338struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
339
340
341extern int device_create_file(struct device *device, struct device_attribute * entry);
342extern void device_remove_file(struct device * dev, struct device_attribute * attr);
343
344
345
346
347
348
349
350extern int (*platform_notify)(struct device * dev);
351
352extern int (*platform_notify_remove)(struct device * dev);
353
354
355
356
357
358
359extern struct device * get_device(struct device * dev);
360extern void put_device(struct device * dev);
361extern struct device *device_find(const char *name, struct bus_type *bus);
362
363
364
365
366struct platform_device {
367 char * name;
368 u32 id;
369 struct device dev;
370 u32 num_resources;
371 struct resource * resource;
372};
373
374#define to_platform_device(x) container_of((x), struct platform_device, dev)
375
376extern int platform_device_register(struct platform_device *);
377extern void platform_device_unregister(struct platform_device *);
378
379extern struct bus_type platform_bus_type;
380extern struct device platform_bus;
381
382extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
383extern int platform_get_irq(struct platform_device *, unsigned int);
384extern int platform_add_devices(struct platform_device **, int);
385
386extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
387
388
389extern void device_shutdown(void);
390
391
392
393extern int firmware_register(struct subsystem *);
394extern void firmware_unregister(struct subsystem *);
395
396
397#define dev_printk(level, dev, format, arg...) \
398 printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
399
400#ifdef DEBUG
401#define dev_dbg(dev, format, arg...) \
402 dev_printk(KERN_DEBUG , dev , format , ## arg)
403#else
404#define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
405#endif
406
407#define dev_err(dev, format, arg...) \
408 dev_printk(KERN_ERR , dev , format , ## arg)
409#define dev_info(dev, format, arg...) \
410 dev_printk(KERN_INFO , dev , format , ## arg)
411#define dev_warn(dev, format, arg...) \
412 dev_printk(KERN_WARNING , dev , format , ## arg)
413
414
415#define MODULE_ALIAS_CHARDEV(major,minor) \
416 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
417#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
418 MODULE_ALIAS("char-major-" __stringify(major) "-*")
419#endif
420