blob: 195b327f3e19d0ec6d4a09bca8656e4b65f9e2f4 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48struct bus_type {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -050049 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
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 {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500100 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct bus_type * bus;
102
103 struct completion unloaded;
104 struct kobject kobj;
105 struct list_head devices;
106
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500107 struct module * owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 int (*probe) (struct device * dev);
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500110 int (*remove) (struct device * dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 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
139
140/*
141 * device classes
142 */
143struct class {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500144 const char * name;
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800145 struct module * owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 struct subsystem subsys;
148 struct list_head children;
149 struct list_head interfaces;
150 struct semaphore sem; /* locks both the children and interfaces lists */
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; /* required */
187 dev_t devt; /* dev_t, creates the sysfs "dev" */
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800188 struct class_device_attribute *devt_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct device * dev; /* not necessary, but nice to have */
190 void * class_data; /* class-specific data */
191
192 char class_id[BUS_ID_SIZE]; /* unique to this class */
193};
194
195static inline void *
196class_get_devdata (struct class_device *dev)
197{
198 return dev->class_data;
199}
200
201static inline void
202class_set_devdata (struct class_device *dev, void *data)
203{
204 dev->class_data = data;
205}
206
207
208extern int class_device_register(struct class_device *);
209extern void class_device_unregister(struct class_device *);
210extern void class_device_initialize(struct class_device *);
211extern int class_device_add(struct class_device *);
212extern void class_device_del(struct class_device *);
213
214extern int class_device_rename(struct class_device *, char *);
215
216extern struct class_device * class_device_get(struct class_device *);
217extern void class_device_put(struct class_device *);
218
219struct class_device_attribute {
220 struct attribute attr;
221 ssize_t (*show)(struct class_device *, char * buf);
222 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
223};
224
225#define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
226struct class_device_attribute class_device_attr_##_name = \
227 __ATTR(_name,_mode,_show,_store)
228
229extern int class_device_create_file(struct class_device *,
230 const struct class_device_attribute *);
231extern void class_device_remove_file(struct class_device *,
232 const struct class_device_attribute *);
233extern int class_device_create_bin_file(struct class_device *,
234 struct bin_attribute *);
235extern void class_device_remove_bin_file(struct class_device *,
236 struct bin_attribute *);
237
238struct class_interface {
239 struct list_head node;
240 struct class *class;
241
242 int (*add) (struct class_device *);
243 void (*remove) (struct class_device *);
244};
245
246extern int class_interface_register(struct class_interface *);
247extern void class_interface_unregister(struct class_interface *);
248
gregkh@suse.dee9ba6362005-03-15 11:54:21 -0800249extern struct class *class_create(struct module *owner, char *name);
250extern void class_destroy(struct class *cls);
251extern struct class_device *class_device_create(struct class *cls, dev_t devt,
252 struct device *device, char *fmt, ...)
253 __attribute__((format(printf,4,5)));
254extern void class_device_destroy(struct class *cls, dev_t devt);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257struct device {
258 struct list_head node; /* node in sibling list */
259 struct list_head bus_list; /* node in bus's 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]; /* position on parent bus */
266
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800267 struct semaphore sem; /* semaphore to synchronize calls to
268 * its driver.
269 */
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct bus_type * bus; /* type of bus device is on */
272 struct device_driver *driver; /* which driver has allocated this
273 device */
274 void *driver_data; /* data private to the driver */
275 void *platform_data; /* Platform specific data (e.g. ACPI,
276 BIOS data relevant to device) */
277 struct dev_pm_info power;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 u64 *dma_mask; /* dma mask (if dma'able device) */
280 u64 coherent_dma_mask;/* Like dma_mask, but for
281 alloc_coherent mappings as
282 not all hardware supports
283 64 bit addresses for consistent
284 allocations such descriptors. */
285
286 struct list_head dma_pools; /* dma pools (if dma'ble) */
287
288 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
289 override */
290
291 void (*release)(struct device * dev);
292};
293
294static inline struct device *
295list_to_dev(struct list_head *node)
296{
297 return list_entry(node, struct device, node);
298}
299
300static inline void *
301dev_get_drvdata (struct device *dev)
302{
303 return dev->driver_data;
304}
305
306static inline void
307dev_set_drvdata (struct device *dev, void *data)
308{
309 dev->driver_data = data;
310}
311
312/*
313 * High level routines for use by the bus drivers
314 */
315extern int device_register(struct device * dev);
316extern void device_unregister(struct device * dev);
317extern void device_initialize(struct device * dev);
318extern int device_add(struct device * dev);
319extern void device_del(struct device * dev);
320extern int device_for_each_child(struct device *, void *,
321 int (*fn)(struct device *, void *));
322
323/*
324 * Manual binding of a device to driver. See drivers/base/bus.c
325 * for information on use.
326 */
327extern int driver_probe_device(struct device_driver * drv, struct device * dev);
328extern void device_bind_driver(struct device * dev);
329extern void device_release_driver(struct device * dev);
330extern int device_attach(struct device * dev);
331extern void driver_attach(struct device_driver * drv);
332
333
334/* driverfs interface for exporting device attributes */
335
336struct device_attribute {
337 struct attribute attr;
338 ssize_t (*show)(struct device * dev, char * buf);
339 ssize_t (*store)(struct device * dev, const char * buf, size_t count);
340};
341
342#define DEVICE_ATTR(_name,_mode,_show,_store) \
343struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
344
345
346extern int device_create_file(struct device *device, struct device_attribute * entry);
347extern void device_remove_file(struct device * dev, struct device_attribute * attr);
348
349/*
350 * Platform "fixup" functions - allow the platform to have their say
351 * about devices and actions that the general device layer doesn't
352 * know about.
353 */
354/* Notify platform of device discovery */
355extern int (*platform_notify)(struct device * dev);
356
357extern int (*platform_notify_remove)(struct device * dev);
358
359
360/**
361 * get_device - atomically increment the reference count for the device.
362 *
363 */
364extern struct device * get_device(struct device * dev);
365extern void put_device(struct device * dev);
366extern struct device *device_find(const char *name, struct bus_type *bus);
367
368
369/* drivers/base/platform.c */
370
371struct platform_device {
Dmitry Torokhov8d790d72005-04-26 02:34:05 -0500372 const char * name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 u32 id;
374 struct device dev;
375 u32 num_resources;
376 struct resource * resource;
377};
378
379#define to_platform_device(x) container_of((x), struct platform_device, dev)
380
381extern int platform_device_register(struct platform_device *);
382extern void platform_device_unregister(struct platform_device *);
383
384extern struct bus_type platform_bus_type;
385extern struct device platform_bus;
386
387extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
388extern int platform_get_irq(struct platform_device *, unsigned int);
389extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
390extern int platform_get_irq_byname(struct platform_device *, char *);
391extern int platform_add_devices(struct platform_device **, int);
392
393extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
394
395/* drivers/base/power.c */
396extern void device_shutdown(void);
397
398
399/* drivers/base/firmware.c */
400extern int firmware_register(struct subsystem *);
401extern void firmware_unregister(struct subsystem *);
402
403/* debugging and troubleshooting/diagnostic helpers. */
404#define dev_printk(level, dev, format, arg...) \
405 printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
406
407#ifdef DEBUG
408#define dev_dbg(dev, format, arg...) \
409 dev_printk(KERN_DEBUG , dev , format , ## arg)
410#else
411#define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
412#endif
413
414#define dev_err(dev, format, arg...) \
415 dev_printk(KERN_ERR , dev , format , ## arg)
416#define dev_info(dev, format, arg...) \
417 dev_printk(KERN_INFO , dev , format , ## arg)
418#define dev_warn(dev, format, arg...) \
419 dev_printk(KERN_WARNING , dev , format , ## arg)
420
421/* Create alias, so I can be autoloaded. */
422#define MODULE_ALIAS_CHARDEV(major,minor) \
423 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
424#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
425 MODULE_ALIAS("char-major-" __stringify(major) "-*")
426#endif /* _DEVICE_H_ */