Remove "#ifdef __KERNEL__" checks from unexported headers
[safe/jmp/linux-2.6] / include / linux / pnp.h
1 /*
2  * Linux Plug and Play Support
3  * Copyright by Adam Belay <ambx1@neo.rr.com>
4  */
5
6 #ifndef _LINUX_PNP_H
7 #define _LINUX_PNP_H
8
9 #include <linux/device.h>
10 #include <linux/list.h>
11 #include <linux/errno.h>
12 #include <linux/mod_devicetable.h>
13
14 #define PNP_MAX_PORT            40
15 #define PNP_MAX_MEM             24
16 #define PNP_MAX_IRQ             2
17 #define PNP_MAX_DMA             2
18 #define PNP_NAME_LEN            50
19
20 struct pnp_protocol;
21 struct pnp_dev;
22
23 /*
24  * Resource Management
25  */
26
27 /* Use these instead of directly reading pnp_dev to get resource information */
28 #define pnp_port_start(dev,bar)   ((dev)->res.port_resource[(bar)].start)
29 #define pnp_port_end(dev,bar)     ((dev)->res.port_resource[(bar)].end)
30 #define pnp_port_flags(dev,bar)   ((dev)->res.port_resource[(bar)].flags)
31 #define pnp_port_valid(dev,bar) \
32         ((pnp_port_flags((dev),(bar)) & (IORESOURCE_IO | IORESOURCE_UNSET)) \
33                 == IORESOURCE_IO)
34 #define pnp_port_len(dev,bar) \
35         ((pnp_port_start((dev),(bar)) == 0 &&   \
36           pnp_port_end((dev),(bar)) ==          \
37           pnp_port_start((dev),(bar))) ? 0 :    \
38                                                 \
39          (pnp_port_end((dev),(bar)) -           \
40           pnp_port_start((dev),(bar)) + 1))
41
42 #define pnp_mem_start(dev,bar)   ((dev)->res.mem_resource[(bar)].start)
43 #define pnp_mem_end(dev,bar)     ((dev)->res.mem_resource[(bar)].end)
44 #define pnp_mem_flags(dev,bar)   ((dev)->res.mem_resource[(bar)].flags)
45 #define pnp_mem_valid(dev,bar) \
46         ((pnp_mem_flags((dev),(bar)) & (IORESOURCE_MEM | IORESOURCE_UNSET)) \
47                 == IORESOURCE_MEM)
48 #define pnp_mem_len(dev,bar) \
49         ((pnp_mem_start((dev),(bar)) == 0 &&    \
50           pnp_mem_end((dev),(bar)) ==           \
51           pnp_mem_start((dev),(bar))) ? 0 :     \
52                                                 \
53          (pnp_mem_end((dev),(bar)) -            \
54           pnp_mem_start((dev),(bar)) + 1))
55
56 #define pnp_irq(dev,bar)         ((dev)->res.irq_resource[(bar)].start)
57 #define pnp_irq_flags(dev,bar)   ((dev)->res.irq_resource[(bar)].flags)
58 #define pnp_irq_valid(dev,bar) \
59         ((pnp_irq_flags((dev),(bar)) & (IORESOURCE_IRQ | IORESOURCE_UNSET)) \
60                 == IORESOURCE_IRQ)
61
62 #define pnp_dma(dev,bar)         ((dev)->res.dma_resource[(bar)].start)
63 #define pnp_dma_flags(dev,bar)   ((dev)->res.dma_resource[(bar)].flags)
64 #define pnp_dma_valid(dev,bar) \
65         ((pnp_dma_flags((dev),(bar)) & (IORESOURCE_DMA | IORESOURCE_UNSET)) \
66                 == IORESOURCE_DMA)
67
68 #define PNP_PORT_FLAG_16BITADDR (1<<0)
69 #define PNP_PORT_FLAG_FIXED     (1<<1)
70
71 struct pnp_port {
72         unsigned short min;     /* min base number */
73         unsigned short max;     /* max base number */
74         unsigned char align;    /* align boundary */
75         unsigned char size;     /* size of range */
76         unsigned char flags;    /* port flags */
77         unsigned char pad;      /* pad */
78         struct pnp_port *next;  /* next port */
79 };
80
81 #define PNP_IRQ_NR 256
82 struct pnp_irq {
83         DECLARE_BITMAP(map, PNP_IRQ_NR);        /* bitmask for IRQ lines */
84         unsigned char flags;    /* IRQ flags */
85         unsigned char pad;      /* pad */
86         struct pnp_irq *next;   /* next IRQ */
87 };
88
89 struct pnp_dma {
90         unsigned char map;      /* bitmask for DMA channels */
91         unsigned char flags;    /* DMA flags */
92         struct pnp_dma *next;   /* next port */
93 };
94
95 struct pnp_mem {
96         unsigned int min;       /* min base number */
97         unsigned int max;       /* max base number */
98         unsigned int align;     /* align boundary */
99         unsigned int size;      /* size of range */
100         unsigned char flags;    /* memory flags */
101         unsigned char pad;      /* pad */
102         struct pnp_mem *next;   /* next memory resource */
103 };
104
105 #define PNP_RES_PRIORITY_PREFERRED      0
106 #define PNP_RES_PRIORITY_ACCEPTABLE     1
107 #define PNP_RES_PRIORITY_FUNCTIONAL     2
108 #define PNP_RES_PRIORITY_INVALID        65535
109
110 struct pnp_option {
111         unsigned short priority;        /* priority */
112         struct pnp_port *port;          /* first port */
113         struct pnp_irq *irq;            /* first IRQ */
114         struct pnp_dma *dma;            /* first DMA */
115         struct pnp_mem *mem;            /* first memory resource */
116         struct pnp_option *next;        /* used to chain dependent resources */
117 };
118
119 struct pnp_resource_table {
120         struct resource port_resource[PNP_MAX_PORT];
121         struct resource mem_resource[PNP_MAX_MEM];
122         struct resource dma_resource[PNP_MAX_DMA];
123         struct resource irq_resource[PNP_MAX_IRQ];
124 };
125
126 /*
127  * Device Management
128  */
129
130 struct pnp_card {
131         struct device dev;              /* Driver Model device interface */
132         unsigned char number;           /* used as an index, must be unique */
133         struct list_head global_list;   /* node in global list of cards */
134         struct list_head protocol_list; /* node in protocol's list of cards */
135         struct list_head devices;       /* devices attached to the card */
136
137         struct pnp_protocol *protocol;
138         struct pnp_id *id;              /* contains supported EISA IDs */
139
140         char name[PNP_NAME_LEN];        /* contains a human-readable name */
141         unsigned char pnpver;           /* Plug & Play version */
142         unsigned char productver;       /* product version */
143         unsigned int serial;            /* serial number */
144         unsigned char checksum;         /* if zero - checksum passed */
145         struct proc_dir_entry *procdir; /* directory entry in /proc/bus/isapnp */
146 };
147
148 #define global_to_pnp_card(n) list_entry(n, struct pnp_card, global_list)
149 #define protocol_to_pnp_card(n) list_entry(n, struct pnp_card, protocol_list)
150 #define to_pnp_card(n) container_of(n, struct pnp_card, dev)
151 #define pnp_for_each_card(card) \
152         for((card) = global_to_pnp_card(pnp_cards.next); \
153         (card) != global_to_pnp_card(&pnp_cards); \
154         (card) = global_to_pnp_card((card)->global_list.next))
155
156 struct pnp_card_link {
157         struct pnp_card *card;
158         struct pnp_card_driver *driver;
159         void *driver_data;
160         pm_message_t pm_state;
161 };
162
163 static inline void *pnp_get_card_drvdata(struct pnp_card_link *pcard)
164 {
165         return pcard->driver_data;
166 }
167
168 static inline void pnp_set_card_drvdata(struct pnp_card_link *pcard, void *data)
169 {
170         pcard->driver_data = data;
171 }
172
173 struct pnp_dev {
174         struct device dev;              /* Driver Model device interface */
175         u64 dma_mask;
176         unsigned int number;            /* used as an index, must be unique */
177         int status;
178
179         struct list_head global_list;   /* node in global list of devices */
180         struct list_head protocol_list; /* node in list of device's protocol */
181         struct list_head card_list;     /* node in card's list of devices */
182         struct list_head rdev_list;     /* node in cards list of requested devices */
183
184         struct pnp_protocol *protocol;
185         struct pnp_card *card;  /* card the device is attached to, none if NULL */
186         struct pnp_driver *driver;
187         struct pnp_card_link *card_link;
188
189         struct pnp_id *id;              /* supported EISA IDs */
190
191         int active;
192         int capabilities;
193         struct pnp_option *independent;
194         struct pnp_option *dependent;
195         struct pnp_resource_table res;
196
197         char name[PNP_NAME_LEN];        /* contains a human-readable name */
198         unsigned short regs;            /* ISAPnP: supported registers */
199         int flags;                      /* used by protocols */
200         struct proc_dir_entry *procent; /* device entry in /proc/bus/isapnp */
201         void *data;
202 };
203
204 #define global_to_pnp_dev(n) list_entry(n, struct pnp_dev, global_list)
205 #define card_to_pnp_dev(n) list_entry(n, struct pnp_dev, card_list)
206 #define protocol_to_pnp_dev(n) list_entry(n, struct pnp_dev, protocol_list)
207 #define to_pnp_dev(n) container_of(n, struct pnp_dev, dev)
208 #define pnp_for_each_dev(dev) \
209         for((dev) = global_to_pnp_dev(pnp_global.next); \
210         (dev) != global_to_pnp_dev(&pnp_global); \
211         (dev) = global_to_pnp_dev((dev)->global_list.next))
212 #define card_for_each_dev(card,dev) \
213         for((dev) = card_to_pnp_dev((card)->devices.next); \
214         (dev) != card_to_pnp_dev(&(card)->devices); \
215         (dev) = card_to_pnp_dev((dev)->card_list.next))
216 #define pnp_dev_name(dev) (dev)->name
217
218 static inline void *pnp_get_drvdata(struct pnp_dev *pdev)
219 {
220         return dev_get_drvdata(&pdev->dev);
221 }
222
223 static inline void pnp_set_drvdata(struct pnp_dev *pdev, void *data)
224 {
225         dev_set_drvdata(&pdev->dev, data);
226 }
227
228 struct pnp_fixup {
229         char id[7];
230         void (*quirk_function) (struct pnp_dev * dev);  /* fixup function */
231 };
232
233 /* config parameters */
234 #define PNP_CONFIG_NORMAL       0x0001
235 #define PNP_CONFIG_FORCE        0x0002  /* disables validity checking */
236
237 /* capabilities */
238 #define PNP_READ                0x0001
239 #define PNP_WRITE               0x0002
240 #define PNP_DISABLE             0x0004
241 #define PNP_CONFIGURABLE        0x0008
242 #define PNP_REMOVABLE           0x0010
243
244 #define pnp_can_read(dev)       (((dev)->protocol->get) && \
245                                  ((dev)->capabilities & PNP_READ))
246 #define pnp_can_write(dev)      (((dev)->protocol->set) && \
247                                  ((dev)->capabilities & PNP_WRITE))
248 #define pnp_can_disable(dev)    (((dev)->protocol->disable) && \
249                                  ((dev)->capabilities & PNP_DISABLE))
250 #define pnp_can_configure(dev)  ((!(dev)->active) && \
251                                  ((dev)->capabilities & PNP_CONFIGURABLE))
252
253 #ifdef CONFIG_ISAPNP
254 extern struct pnp_protocol isapnp_protocol;
255 #define pnp_device_is_isapnp(dev) ((dev)->protocol == (&isapnp_protocol))
256 #else
257 #define pnp_device_is_isapnp(dev) 0
258 #endif
259 extern struct mutex pnp_res_mutex;
260
261 #ifdef CONFIG_PNPBIOS
262 extern struct pnp_protocol pnpbios_protocol;
263 #define pnp_device_is_pnpbios(dev) ((dev)->protocol == (&pnpbios_protocol))
264 #else
265 #define pnp_device_is_pnpbios(dev) 0
266 #endif
267
268 /* status */
269 #define PNP_READY               0x0000
270 #define PNP_ATTACHED            0x0001
271 #define PNP_BUSY                0x0002
272 #define PNP_FAULTY              0x0004
273
274 /* isapnp specific macros */
275
276 #define isapnp_card_number(dev) ((dev)->card ? (dev)->card->number : -1)
277 #define isapnp_csn_number(dev)  ((dev)->number)
278
279 /*
280  * Driver Management
281  */
282
283 struct pnp_id {
284         char id[PNP_ID_LEN];
285         struct pnp_id *next;
286 };
287
288 struct pnp_driver {
289         char *name;
290         const struct pnp_device_id *id_table;
291         unsigned int flags;
292         int (*probe) (struct pnp_dev *dev, const struct pnp_device_id *dev_id);
293         void (*remove) (struct pnp_dev *dev);
294         int (*suspend) (struct pnp_dev *dev, pm_message_t state);
295         int (*resume) (struct pnp_dev *dev);
296         struct device_driver driver;
297 };
298
299 #define to_pnp_driver(drv) container_of(drv, struct pnp_driver, driver)
300
301 struct pnp_card_driver {
302         struct list_head global_list;
303         char *name;
304         const struct pnp_card_device_id *id_table;
305         unsigned int flags;
306         int (*probe) (struct pnp_card_link *card,
307                       const struct pnp_card_device_id *card_id);
308         void (*remove) (struct pnp_card_link *card);
309         int (*suspend) (struct pnp_card_link *card, pm_message_t state);
310         int (*resume) (struct pnp_card_link *card);
311         struct pnp_driver link;
312 };
313
314 #define to_pnp_card_driver(drv) container_of(drv, struct pnp_card_driver, link)
315
316 /* pnp driver flags */
317 #define PNP_DRIVER_RES_DO_NOT_CHANGE    0x0001  /* do not change the state of the device */
318 #define PNP_DRIVER_RES_DISABLE          0x0003  /* ensure the device is disabled */
319
320 /*
321  * Protocol Management
322  */
323
324 struct pnp_protocol {
325         struct list_head protocol_list;
326         char *name;
327
328         /* resource control functions */
329         int (*get) (struct pnp_dev *dev, struct pnp_resource_table *res);
330         int (*set) (struct pnp_dev *dev, struct pnp_resource_table *res);
331         int (*disable) (struct pnp_dev *dev);
332
333         /* protocol specific suspend/resume */
334         int (*suspend) (struct pnp_dev * dev, pm_message_t state);
335         int (*resume) (struct pnp_dev * dev);
336
337         /* used by pnp layer only (look but don't touch) */
338         unsigned char number;   /* protocol number */
339         struct device dev;      /* link to driver model */
340         struct list_head cards;
341         struct list_head devices;
342 };
343
344 #define to_pnp_protocol(n) list_entry(n, struct pnp_protocol, protocol_list)
345 #define protocol_for_each_card(protocol,card) \
346         for((card) = protocol_to_pnp_card((protocol)->cards.next); \
347         (card) != protocol_to_pnp_card(&(protocol)->cards); \
348         (card) = protocol_to_pnp_card((card)->protocol_list.next))
349 #define protocol_for_each_dev(protocol,dev) \
350         for((dev) = protocol_to_pnp_dev((protocol)->devices.next); \
351         (dev) != protocol_to_pnp_dev(&(protocol)->devices); \
352         (dev) = protocol_to_pnp_dev((dev)->protocol_list.next))
353
354 extern struct bus_type pnp_bus_type;
355
356 #if defined(CONFIG_PNP)
357
358 /* device management */
359 int pnp_register_protocol(struct pnp_protocol *protocol);
360 void pnp_unregister_protocol(struct pnp_protocol *protocol);
361 int pnp_add_device(struct pnp_dev *dev);
362 int pnp_device_attach(struct pnp_dev *pnp_dev);
363 void pnp_device_detach(struct pnp_dev *pnp_dev);
364 extern struct list_head pnp_global;
365 extern int pnp_platform_devices;
366
367 /* multidevice card support */
368 int pnp_add_card(struct pnp_card *card);
369 void pnp_remove_card(struct pnp_card *card);
370 int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev);
371 void pnp_remove_card_device(struct pnp_dev *dev);
372 int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card);
373 struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
374                                         const char *id, struct pnp_dev *from);
375 void pnp_release_card_device(struct pnp_dev *dev);
376 int pnp_register_card_driver(struct pnp_card_driver *drv);
377 void pnp_unregister_card_driver(struct pnp_card_driver *drv);
378 extern struct list_head pnp_cards;
379
380 /* resource management */
381 struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev);
382 struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
383                                                  int priority);
384 int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data);
385 int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data);
386 int pnp_register_port_resource(struct pnp_option *option,
387                                struct pnp_port *data);
388 int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data);
389 void pnp_init_resource_table(struct pnp_resource_table *table);
390 int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
391                           int mode);
392 int pnp_auto_config_dev(struct pnp_dev *dev);
393 int pnp_validate_config(struct pnp_dev *dev);
394 int pnp_start_dev(struct pnp_dev *dev);
395 int pnp_stop_dev(struct pnp_dev *dev);
396 int pnp_activate_dev(struct pnp_dev *dev);
397 int pnp_disable_dev(struct pnp_dev *dev);
398 void pnp_resource_change(struct resource *resource, resource_size_t start,
399                          resource_size_t size);
400
401 /* protocol helpers */
402 int pnp_is_active(struct pnp_dev *dev);
403 int compare_pnp_id(struct pnp_id *pos, const char *id);
404 int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev);
405 int pnp_register_driver(struct pnp_driver *drv);
406 void pnp_unregister_driver(struct pnp_driver *drv);
407
408 #else
409
410 /* device management */
411 static inline int pnp_register_protocol(struct pnp_protocol *protocol) { return -ENODEV; }
412 static inline void pnp_unregister_protocol(struct pnp_protocol *protocol) { }
413 static inline int pnp_init_device(struct pnp_dev *dev) { return -ENODEV; }
414 static inline int pnp_add_device(struct pnp_dev *dev) { return -ENODEV; }
415 static inline int pnp_device_attach(struct pnp_dev *pnp_dev) { return -ENODEV; }
416 static inline void pnp_device_detach(struct pnp_dev *pnp_dev) { }
417
418 #define pnp_platform_devices 0
419
420 /* multidevice card support */
421 static inline int pnp_add_card(struct pnp_card *card) { return -ENODEV; }
422 static inline void pnp_remove_card(struct pnp_card *card) { }
423 static inline int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev) { return -ENODEV; }
424 static inline void pnp_remove_card_device(struct pnp_dev *dev) { }
425 static inline int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card) { return -ENODEV; }
426 static inline struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink, const char *id, struct pnp_dev *from) { return NULL; }
427 static inline void pnp_release_card_device(struct pnp_dev *dev) { }
428 static inline int pnp_register_card_driver(struct pnp_card_driver *drv) { return -ENODEV; }
429 static inline void pnp_unregister_card_driver(struct pnp_card_driver *drv) { }
430
431 /* resource management */
432 static inline struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev) { return NULL; }
433 static inline struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev, int priority) { return NULL; }
434 static inline int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data) { return -ENODEV; }
435 static inline int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data) { return -ENODEV; }
436 static inline int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data) { return -ENODEV; }
437 static inline int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data) { return -ENODEV; }
438 static inline void pnp_init_resource_table(struct pnp_resource_table *table) { }
439 static inline int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res, int mode) { return -ENODEV; }
440 static inline int pnp_auto_config_dev(struct pnp_dev *dev) { return -ENODEV; }
441 static inline int pnp_validate_config(struct pnp_dev *dev) { return -ENODEV; }
442 static inline int pnp_start_dev(struct pnp_dev *dev) { return -ENODEV; }
443 static inline int pnp_stop_dev(struct pnp_dev *dev) { return -ENODEV; }
444 static inline int pnp_activate_dev(struct pnp_dev *dev) { return -ENODEV; }
445 static inline int pnp_disable_dev(struct pnp_dev *dev) { return -ENODEV; }
446 static inline void pnp_resource_change(struct resource *resource, resource_size_t start, resource_size_t size) { }
447
448 /* protocol helpers */
449 static inline int pnp_is_active(struct pnp_dev *dev) { return 0; }
450 static inline int compare_pnp_id(struct pnp_id *pos, const char *id) { return -ENODEV; }
451 static inline int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev) { return -ENODEV; }
452 static inline int pnp_register_driver(struct pnp_driver *drv) { return -ENODEV; }
453 static inline void pnp_unregister_driver(struct pnp_driver *drv) { }
454
455 #endif /* CONFIG_PNP */
456
457 #define pnp_err(format, arg...) printk(KERN_ERR "pnp: " format "\n" , ## arg)
458 #define pnp_info(format, arg...) printk(KERN_INFO "pnp: " format "\n" , ## arg)
459 #define pnp_warn(format, arg...) printk(KERN_WARNING "pnp: " format "\n" , ## arg)
460
461 #ifdef CONFIG_PNP_DEBUG
462 #define pnp_dbg(format, arg...) printk(KERN_DEBUG "pnp: " format "\n" , ## arg)
463 #else
464 #define pnp_dbg(format, arg...) do {} while (0)
465 #endif
466
467 #endif /* _LINUX_PNP_H */