blob: 73250d01c01ffe2a9be04ce52631e82e61a8750a [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>
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;
47struct class_simple;
48
49struct bus_type {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -050050 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 struct subsystem subsys;
53 struct kset drivers;
54 struct kset devices;
55
56 struct bus_attribute * bus_attrs;
57 struct device_attribute * dev_attrs;
58 struct driver_attribute * drv_attrs;
59
60 int (*match)(struct device * dev, struct device_driver * drv);
61 int (*hotplug) (struct device *dev, char **envp,
62 int num_envp, char *buffer, int buffer_size);
63 int (*suspend)(struct device * dev, pm_message_t state);
64 int (*resume)(struct device * dev);
65};
66
67extern int bus_register(struct bus_type * bus);
68extern void bus_unregister(struct bus_type * bus);
69
70extern int bus_rescan_devices(struct bus_type * bus);
71
72extern struct bus_type * get_bus(struct bus_type * bus);
73extern void put_bus(struct bus_type * bus);
74
75extern struct bus_type * find_bus(char * name);
76
77/* iterator helpers for buses */
78
79int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
80 int (*fn)(struct device *, void *));
81
82int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
83 void * data, int (*fn)(struct device_driver *, void *));
84
85
86/* driverfs interface for exporting bus attributes */
87
88struct bus_attribute {
89 struct attribute attr;
90 ssize_t (*show)(struct bus_type *, char * buf);
91 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
92};
93
94#define BUS_ATTR(_name,_mode,_show,_store) \
95struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
96
97extern int bus_create_file(struct bus_type *, struct bus_attribute *);
98extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
99
100struct device_driver {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500101 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct bus_type * bus;
103
104 struct completion unloaded;
105 struct kobject kobj;
106 struct list_head devices;
107
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500108 struct module * owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 int (*probe) (struct device * dev);
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500111 int (*remove) (struct device * dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 void (*shutdown) (struct device * dev);
113 int (*suspend) (struct device * dev, pm_message_t 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/* driverfs interface for exporting driver attributes */
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 * device classes
143 */
144struct class {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500145 const char * name;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800146 struct module * owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 struct subsystem subsys;
149 struct list_head children;
150 struct list_head interfaces;
151 struct semaphore sem; /* locks both the children and interfaces lists */
152
153 struct class_attribute * class_attrs;
154 struct class_device_attribute * class_dev_attrs;
155
156 int (*hotplug)(struct class_device *dev, char **envp,
157 int num_envp, char *buffer, int buffer_size);
158
159 void (*release)(struct class_device *dev);
160 void (*class_release)(struct class *class);
161};
162
163extern int class_register(struct class *);
164extern void class_unregister(struct class *);
165
166extern struct class * class_get(struct class *);
167extern void class_put(struct class *);
168
169
170struct class_attribute {
171 struct attribute attr;
172 ssize_t (*show)(struct class *, char * buf);
173 ssize_t (*store)(struct class *, const char * buf, size_t count);
174};
175
176#define CLASS_ATTR(_name,_mode,_show,_store) \
177struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
178
179extern int class_create_file(struct class *, const struct class_attribute *);
180extern void class_remove_file(struct class *, const struct class_attribute *);
181
182
183struct class_device {
184 struct list_head node;
185
186 struct kobject kobj;
187 struct class * class; /* required */
188 dev_t devt; /* dev_t, creates the sysfs "dev" */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800189 struct class_device_attribute *devt_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct device * dev; /* not necessary, but nice to have */
191 void * class_data; /* class-specific data */
192
193 char class_id[BUS_ID_SIZE]; /* unique to this class */
194};
195
196static inline void *
197class_get_devdata (struct class_device *dev)
198{
199 return dev->class_data;
200}
201
202static inline void
203class_set_devdata (struct class_device *dev, void *data)
204{
205 dev->class_data = data;
206}
207
208
209extern int class_device_register(struct class_device *);
210extern void class_device_unregister(struct class_device *);
211extern void class_device_initialize(struct class_device *);
212extern int class_device_add(struct class_device *);
213extern void class_device_del(struct class_device *);
214
215extern int class_device_rename(struct class_device *, char *);
216
217extern struct class_device * class_device_get(struct class_device *);
218extern void class_device_put(struct class_device *);
219
220struct class_device_attribute {
221 struct attribute attr;
222 ssize_t (*show)(struct class_device *, char * buf);
223 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
224};
225
226#define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
227struct class_device_attribute class_device_attr_##_name = \
228 __ATTR(_name,_mode,_show,_store)
229
230extern int class_device_create_file(struct class_device *,
231 const struct class_device_attribute *);
232extern void class_device_remove_file(struct class_device *,
233 const struct class_device_attribute *);
234extern int class_device_create_bin_file(struct class_device *,
235 struct bin_attribute *);
236extern void class_device_remove_bin_file(struct class_device *,
237 struct bin_attribute *);
238
239struct class_interface {
240 struct list_head node;
241 struct class *class;
242
243 int (*add) (struct class_device *);
244 void (*remove) (struct class_device *);
245};
246
247extern int class_interface_register(struct class_interface *);
248extern void class_interface_unregister(struct class_interface *);
249
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800250extern struct class *class_create(struct module *owner, char *name);
251extern void class_destroy(struct class *cls);
252extern struct class_device *class_device_create(struct class *cls, dev_t devt,
253 struct device *device, char *fmt, ...)
254 __attribute__((format(printf,4,5)));
255extern void class_device_destroy(struct class *cls, dev_t devt);
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/* interface for class simple stuff */
258extern struct class_simple *class_simple_create(struct module *owner, char *name);
259extern void class_simple_destroy(struct class_simple *cs);
260extern struct class_device *class_simple_device_add(struct class_simple *cs, dev_t dev, struct device *device, const char *fmt, ...)
261 __attribute__((format(printf,4,5)));
262extern int class_simple_set_hotplug(struct class_simple *,
263 int (*hotplug)(struct class_device *dev, char **envp, int num_envp, char *buffer, int buffer_size));
264extern void class_simple_device_remove(dev_t dev);
265
266
267struct device {
268 struct list_head node; /* node in sibling list */
269 struct list_head bus_list; /* node in bus's list */
270 struct list_head driver_list;
271 struct list_head children;
272 struct device * parent;
273
274 struct kobject kobj;
275 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
276
277 struct bus_type * bus; /* type of bus device is on */
278 struct device_driver *driver; /* which driver has allocated this
279 device */
280 void *driver_data; /* data private to the driver */
281 void *platform_data; /* Platform specific data (e.g. ACPI,
282 BIOS data relevant to device) */
283 struct dev_pm_info power;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 u64 *dma_mask; /* dma mask (if dma'able device) */
286 u64 coherent_dma_mask;/* Like dma_mask, but for
287 alloc_coherent mappings as
288 not all hardware supports
289 64 bit addresses for consistent
290 allocations such descriptors. */
291
292 struct list_head dma_pools; /* dma pools (if dma'ble) */
293
294 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
295 override */
296
297 void (*release)(struct device * dev);
298};
299
300static inline struct device *
301list_to_dev(struct list_head *node)
302{
303 return list_entry(node, struct device, node);
304}
305
306static inline void *
307dev_get_drvdata (struct device *dev)
308{
309 return dev->driver_data;
310}
311
312static inline void
313dev_set_drvdata (struct device *dev, void *data)
314{
315 dev->driver_data = data;
316}
317
318/*
319 * High level routines for use by the bus drivers
320 */
321extern int device_register(struct device * dev);
322extern void device_unregister(struct device * dev);
323extern void device_initialize(struct device * dev);
324extern int device_add(struct device * dev);
325extern void device_del(struct device * dev);
326extern int device_for_each_child(struct device *, void *,
327 int (*fn)(struct device *, void *));
328
329/*
330 * Manual binding of a device to driver. See drivers/base/bus.c
331 * for information on use.
332 */
333extern int driver_probe_device(struct device_driver * drv, struct device * dev);
334extern void device_bind_driver(struct device * dev);
335extern void device_release_driver(struct device * dev);
336extern int device_attach(struct device * dev);
337extern void driver_attach(struct device_driver * drv);
338
339
340/* driverfs interface for exporting device attributes */
341
342struct device_attribute {
343 struct attribute attr;
344 ssize_t (*show)(struct device * dev, char * buf);
345 ssize_t (*store)(struct device * dev, const char * buf, size_t count);
346};
347
348#define DEVICE_ATTR(_name,_mode,_show,_store) \
349struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
350
351
352extern int device_create_file(struct device *device, struct device_attribute * entry);
353extern void device_remove_file(struct device * dev, struct device_attribute * attr);
354
355/*
356 * Platform "fixup" functions - allow the platform to have their say
357 * about devices and actions that the general device layer doesn't
358 * know about.
359 */
360/* Notify platform of device discovery */
361extern int (*platform_notify)(struct device * dev);
362
363extern int (*platform_notify_remove)(struct device * dev);
364
365
366/**
367 * get_device - atomically increment the reference count for the device.
368 *
369 */
370extern struct device * get_device(struct device * dev);
371extern void put_device(struct device * dev);
372extern struct device *device_find(const char *name, struct bus_type *bus);
373
374
375/* drivers/base/platform.c */
376
377struct platform_device {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500378 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 u32 id;
380 struct device dev;
381 u32 num_resources;
382 struct resource * resource;
383};
384
385#define to_platform_device(x) container_of((x), struct platform_device, dev)
386
387extern int platform_device_register(struct platform_device *);
388extern void platform_device_unregister(struct platform_device *);
389
390extern struct bus_type platform_bus_type;
391extern struct device platform_bus;
392
393extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
394extern int platform_get_irq(struct platform_device *, unsigned int);
395extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
396extern int platform_get_irq_byname(struct platform_device *, char *);
397extern int platform_add_devices(struct platform_device **, int);
398
399extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
400
401/* drivers/base/power.c */
402extern void device_shutdown(void);
403
404
405/* drivers/base/firmware.c */
406extern int firmware_register(struct subsystem *);
407extern void firmware_unregister(struct subsystem *);
408
409/* debugging and troubleshooting/diagnostic helpers. */
410#define dev_printk(level, dev, format, arg...) \
411 printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
412
413#ifdef DEBUG
414#define dev_dbg(dev, format, arg...) \
415 dev_printk(KERN_DEBUG , dev , format , ## arg)
416#else
417#define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
418#endif
419
420#define dev_err(dev, format, arg...) \
421 dev_printk(KERN_ERR , dev , format , ## arg)
422#define dev_info(dev, format, arg...) \
423 dev_printk(KERN_INFO , dev , format , ## arg)
424#define dev_warn(dev, format, arg...) \
425 dev_printk(KERN_WARNING , dev , format , ## arg)
426
427/* Create alias, so I can be autoloaded. */
428#define MODULE_ALIAS_CHARDEV(major,minor) \
429 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
430#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
431 MODULE_ALIAS("char-major-" __stringify(major) "-*")
432#endif /* _DEVICE_H_ */