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