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