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