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>
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>
17 #define PNP_NAME_LEN 50
26 struct resource *pnp_get_resource(struct pnp_dev *dev, unsigned long type,
29 static inline struct resource *pnp_get_resource(struct pnp_dev *dev,
30 unsigned long type, unsigned int num)
36 static inline int pnp_resource_valid(struct resource *res)
43 static inline int pnp_resource_enabled(struct resource *res)
45 if (res && !(res->flags & IORESOURCE_DISABLED))
50 static inline resource_size_t pnp_resource_len(struct resource *res)
52 if (res->start == 0 && res->end == 0)
54 return resource_size(res);
58 static inline resource_size_t pnp_port_start(struct pnp_dev *dev,
61 struct resource *res = pnp_get_resource(dev, IORESOURCE_IO, bar);
63 if (pnp_resource_valid(res))
68 static inline resource_size_t pnp_port_end(struct pnp_dev *dev,
71 struct resource *res = pnp_get_resource(dev, IORESOURCE_IO, bar);
73 if (pnp_resource_valid(res))
78 static inline unsigned long pnp_port_flags(struct pnp_dev *dev,
81 struct resource *res = pnp_get_resource(dev, IORESOURCE_IO, bar);
83 if (pnp_resource_valid(res))
85 return IORESOURCE_IO | IORESOURCE_AUTO;
88 static inline int pnp_port_valid(struct pnp_dev *dev, unsigned int bar)
90 return pnp_resource_valid(pnp_get_resource(dev, IORESOURCE_IO, bar));
93 static inline resource_size_t pnp_port_len(struct pnp_dev *dev,
96 struct resource *res = pnp_get_resource(dev, IORESOURCE_IO, bar);
98 if (pnp_resource_valid(res))
99 return pnp_resource_len(res);
104 static inline resource_size_t pnp_mem_start(struct pnp_dev *dev,
107 struct resource *res = pnp_get_resource(dev, IORESOURCE_MEM, bar);
109 if (pnp_resource_valid(res))
114 static inline resource_size_t pnp_mem_end(struct pnp_dev *dev,
117 struct resource *res = pnp_get_resource(dev, IORESOURCE_MEM, bar);
119 if (pnp_resource_valid(res))
124 static inline unsigned long pnp_mem_flags(struct pnp_dev *dev, unsigned int bar)
126 struct resource *res = pnp_get_resource(dev, IORESOURCE_MEM, bar);
128 if (pnp_resource_valid(res))
130 return IORESOURCE_MEM | IORESOURCE_AUTO;
133 static inline int pnp_mem_valid(struct pnp_dev *dev, unsigned int bar)
135 return pnp_resource_valid(pnp_get_resource(dev, IORESOURCE_MEM, bar));
138 static inline resource_size_t pnp_mem_len(struct pnp_dev *dev,
141 struct resource *res = pnp_get_resource(dev, IORESOURCE_MEM, bar);
143 if (pnp_resource_valid(res))
144 return pnp_resource_len(res);
149 static inline resource_size_t pnp_irq(struct pnp_dev *dev, unsigned int bar)
151 struct resource *res = pnp_get_resource(dev, IORESOURCE_IRQ, bar);
153 if (pnp_resource_valid(res))
158 static inline unsigned long pnp_irq_flags(struct pnp_dev *dev, unsigned int bar)
160 struct resource *res = pnp_get_resource(dev, IORESOURCE_IRQ, bar);
162 if (pnp_resource_valid(res))
164 return IORESOURCE_IRQ | IORESOURCE_AUTO;
167 static inline int pnp_irq_valid(struct pnp_dev *dev, unsigned int bar)
169 return pnp_resource_valid(pnp_get_resource(dev, IORESOURCE_IRQ, bar));
173 static inline resource_size_t pnp_dma(struct pnp_dev *dev, unsigned int bar)
175 struct resource *res = pnp_get_resource(dev, IORESOURCE_DMA, bar);
177 if (pnp_resource_valid(res))
182 static inline unsigned long pnp_dma_flags(struct pnp_dev *dev, unsigned int bar)
184 struct resource *res = pnp_get_resource(dev, IORESOURCE_DMA, bar);
186 if (pnp_resource_valid(res))
188 return IORESOURCE_DMA | IORESOURCE_AUTO;
191 static inline int pnp_dma_valid(struct pnp_dev *dev, unsigned int bar)
193 return pnp_resource_valid(pnp_get_resource(dev, IORESOURCE_DMA, bar));
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 */
208 struct pnp_protocol *protocol;
209 struct pnp_id *id; /* contains supported EISA IDs */
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 */
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 for((card) = global_to_pnp_card(pnp_cards.next); \
224 (card) != global_to_pnp_card(&pnp_cards); \
225 (card) = global_to_pnp_card((card)->global_list.next))
227 struct pnp_card_link {
228 struct pnp_card *card;
229 struct pnp_card_driver *driver;
231 pm_message_t pm_state;
234 static inline void *pnp_get_card_drvdata(struct pnp_card_link *pcard)
236 return pcard->driver_data;
239 static inline void pnp_set_card_drvdata(struct pnp_card_link *pcard, void *data)
241 pcard->driver_data = data;
245 struct device dev; /* Driver Model device interface */
247 unsigned int number; /* used as an index, must be unique */
250 struct list_head global_list; /* node in global list of devices */
251 struct list_head protocol_list; /* node in list of device's protocol */
252 struct list_head card_list; /* node in card's list of devices */
253 struct list_head rdev_list; /* node in cards list of requested devices */
255 struct pnp_protocol *protocol;
256 struct pnp_card *card; /* card the device is attached to, none if NULL */
257 struct pnp_driver *driver;
258 struct pnp_card_link *card_link;
260 struct pnp_id *id; /* supported EISA IDs */
264 unsigned int num_dependent_sets;
265 struct list_head resources;
266 struct list_head options;
268 char name[PNP_NAME_LEN]; /* contains a human-readable name */
269 int flags; /* used by protocols */
270 struct proc_dir_entry *procent; /* device entry in /proc/bus/isapnp */
274 #define global_to_pnp_dev(n) list_entry(n, struct pnp_dev, global_list)
275 #define card_to_pnp_dev(n) list_entry(n, struct pnp_dev, card_list)
276 #define protocol_to_pnp_dev(n) list_entry(n, struct pnp_dev, protocol_list)
277 #define to_pnp_dev(n) container_of(n, struct pnp_dev, dev)
278 #define pnp_for_each_dev(dev) \
279 for((dev) = global_to_pnp_dev(pnp_global.next); \
280 (dev) != global_to_pnp_dev(&pnp_global); \
281 (dev) = global_to_pnp_dev((dev)->global_list.next))
282 #define card_for_each_dev(card,dev) \
283 for((dev) = card_to_pnp_dev((card)->devices.next); \
284 (dev) != card_to_pnp_dev(&(card)->devices); \
285 (dev) = card_to_pnp_dev((dev)->card_list.next))
286 #define pnp_dev_name(dev) (dev)->name
288 static inline void *pnp_get_drvdata(struct pnp_dev *pdev)
290 return dev_get_drvdata(&pdev->dev);
293 static inline void pnp_set_drvdata(struct pnp_dev *pdev, void *data)
295 dev_set_drvdata(&pdev->dev, data);
300 void (*quirk_function) (struct pnp_dev * dev); /* fixup function */
303 /* config parameters */
304 #define PNP_CONFIG_NORMAL 0x0001
305 #define PNP_CONFIG_FORCE 0x0002 /* disables validity checking */
308 #define PNP_READ 0x0001
309 #define PNP_WRITE 0x0002
310 #define PNP_DISABLE 0x0004
311 #define PNP_CONFIGURABLE 0x0008
312 #define PNP_REMOVABLE 0x0010
313 #define PNP_CONSOLE 0x0020
315 #define pnp_can_read(dev) (((dev)->protocol->get) && \
316 ((dev)->capabilities & PNP_READ))
317 #define pnp_can_write(dev) (((dev)->protocol->set) && \
318 ((dev)->capabilities & PNP_WRITE))
319 #define pnp_can_disable(dev) (((dev)->protocol->disable) && \
320 ((dev)->capabilities & PNP_DISABLE) && \
321 (!((dev)->capabilities & PNP_CONSOLE) || \
322 console_suspend_enabled))
323 #define pnp_can_configure(dev) ((!(dev)->active) && \
324 ((dev)->capabilities & PNP_CONFIGURABLE))
325 #define pnp_can_suspend(dev) (((dev)->protocol->suspend) && \
326 (!((dev)->capabilities & PNP_CONSOLE) || \
327 console_suspend_enabled))
331 extern struct pnp_protocol isapnp_protocol;
332 #define pnp_device_is_isapnp(dev) ((dev)->protocol == (&isapnp_protocol))
334 #define pnp_device_is_isapnp(dev) 0
336 extern struct mutex pnp_res_mutex;
338 #ifdef CONFIG_PNPBIOS
339 extern struct pnp_protocol pnpbios_protocol;
340 #define pnp_device_is_pnpbios(dev) ((dev)->protocol == (&pnpbios_protocol))
342 #define pnp_device_is_pnpbios(dev) 0
345 #ifdef CONFIG_PNPACPI
346 extern struct pnp_protocol pnpacpi_protocol;
348 static inline struct acpi_device *pnp_acpi_device(struct pnp_dev *dev)
350 if (dev->protocol == &pnpacpi_protocol)
355 #define pnp_acpi_device(dev) 0
359 #define PNP_READY 0x0000
360 #define PNP_ATTACHED 0x0001
361 #define PNP_BUSY 0x0002
362 #define PNP_FAULTY 0x0004
364 /* isapnp specific macros */
366 #define isapnp_card_number(dev) ((dev)->card ? (dev)->card->number : -1)
367 #define isapnp_csn_number(dev) ((dev)->number)
380 const struct pnp_device_id *id_table;
382 int (*probe) (struct pnp_dev *dev, const struct pnp_device_id *dev_id);
383 void (*remove) (struct pnp_dev *dev);
384 void (*shutdown) (struct pnp_dev *dev);
385 int (*suspend) (struct pnp_dev *dev, pm_message_t state);
386 int (*resume) (struct pnp_dev *dev);
387 struct device_driver driver;
390 #define to_pnp_driver(drv) container_of(drv, struct pnp_driver, driver)
392 struct pnp_card_driver {
393 struct list_head global_list;
395 const struct pnp_card_device_id *id_table;
397 int (*probe) (struct pnp_card_link *card,
398 const struct pnp_card_device_id *card_id);
399 void (*remove) (struct pnp_card_link *card);
400 int (*suspend) (struct pnp_card_link *card, pm_message_t state);
401 int (*resume) (struct pnp_card_link *card);
402 struct pnp_driver link;
405 #define to_pnp_card_driver(drv) container_of(drv, struct pnp_card_driver, link)
407 /* pnp driver flags */
408 #define PNP_DRIVER_RES_DO_NOT_CHANGE 0x0001 /* do not change the state of the device */
409 #define PNP_DRIVER_RES_DISABLE 0x0003 /* ensure the device is disabled */
412 * Protocol Management
415 struct pnp_protocol {
416 struct list_head protocol_list;
419 /* resource control functions */
420 int (*get) (struct pnp_dev *dev);
421 int (*set) (struct pnp_dev *dev);
422 int (*disable) (struct pnp_dev *dev);
424 /* protocol specific suspend/resume */
425 bool (*can_wakeup) (struct pnp_dev *dev);
426 int (*suspend) (struct pnp_dev * dev, pm_message_t state);
427 int (*resume) (struct pnp_dev * dev);
429 /* used by pnp layer only (look but don't touch) */
430 unsigned char number; /* protocol number */
431 struct device dev; /* link to driver model */
432 struct list_head cards;
433 struct list_head devices;
436 #define to_pnp_protocol(n) list_entry(n, struct pnp_protocol, protocol_list)
437 #define protocol_for_each_card(protocol,card) \
438 for((card) = protocol_to_pnp_card((protocol)->cards.next); \
439 (card) != protocol_to_pnp_card(&(protocol)->cards); \
440 (card) = protocol_to_pnp_card((card)->protocol_list.next))
441 #define protocol_for_each_dev(protocol,dev) \
442 for((dev) = protocol_to_pnp_dev((protocol)->devices.next); \
443 (dev) != protocol_to_pnp_dev(&(protocol)->devices); \
444 (dev) = protocol_to_pnp_dev((dev)->protocol_list.next))
446 extern struct bus_type pnp_bus_type;
448 #if defined(CONFIG_PNP)
450 /* device management */
451 int pnp_device_attach(struct pnp_dev *pnp_dev);
452 void pnp_device_detach(struct pnp_dev *pnp_dev);
453 extern struct list_head pnp_global;
454 extern int pnp_platform_devices;
456 /* multidevice card support */
457 struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
458 const char *id, struct pnp_dev *from);
459 void pnp_release_card_device(struct pnp_dev *dev);
460 int pnp_register_card_driver(struct pnp_card_driver *drv);
461 void pnp_unregister_card_driver(struct pnp_card_driver *drv);
462 extern struct list_head pnp_cards;
464 /* resource management */
465 int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t base,
466 resource_size_t size);
467 int pnp_auto_config_dev(struct pnp_dev *dev);
468 int pnp_start_dev(struct pnp_dev *dev);
469 int pnp_stop_dev(struct pnp_dev *dev);
470 int pnp_activate_dev(struct pnp_dev *dev);
471 int pnp_disable_dev(struct pnp_dev *dev);
472 int pnp_range_reserved(resource_size_t start, resource_size_t end);
474 /* protocol helpers */
475 int pnp_is_active(struct pnp_dev *dev);
476 int compare_pnp_id(struct pnp_id *pos, const char *id);
477 int pnp_register_driver(struct pnp_driver *drv);
478 void pnp_unregister_driver(struct pnp_driver *drv);
482 /* device management */
483 static inline int pnp_device_attach(struct pnp_dev *pnp_dev) { return -ENODEV; }
484 static inline void pnp_device_detach(struct pnp_dev *pnp_dev) { }
486 #define pnp_platform_devices 0
488 /* multidevice card support */
489 static inline struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink, const char *id, struct pnp_dev *from) { return NULL; }
490 static inline void pnp_release_card_device(struct pnp_dev *dev) { }
491 static inline int pnp_register_card_driver(struct pnp_card_driver *drv) { return -ENODEV; }
492 static inline void pnp_unregister_card_driver(struct pnp_card_driver *drv) { }
494 /* resource management */
495 static inline int pnp_possible_config(struct pnp_dev *dev, int type,
496 resource_size_t base,
497 resource_size_t size) { return 0; }
498 static inline int pnp_auto_config_dev(struct pnp_dev *dev) { return -ENODEV; }
499 static inline int pnp_start_dev(struct pnp_dev *dev) { return -ENODEV; }
500 static inline int pnp_stop_dev(struct pnp_dev *dev) { return -ENODEV; }
501 static inline int pnp_activate_dev(struct pnp_dev *dev) { return -ENODEV; }
502 static inline int pnp_disable_dev(struct pnp_dev *dev) { return -ENODEV; }
503 static inline int pnp_range_reserved(resource_size_t start, resource_size_t end) { return 0;}
505 /* protocol helpers */
506 static inline int pnp_is_active(struct pnp_dev *dev) { return 0; }
507 static inline int compare_pnp_id(struct pnp_id *pos, const char *id) { return -ENODEV; }
508 static inline int pnp_register_driver(struct pnp_driver *drv) { return -ENODEV; }
509 static inline void pnp_unregister_driver(struct pnp_driver *drv) { }
511 #endif /* CONFIG_PNP */
514 * module_pnp_driver() - Helper macro for registering a PnP driver
515 * @__pnp_driver: pnp_driver struct
517 * Helper macro for PnP drivers which do not do anything special in module
518 * init/exit. This eliminates a lot of boilerplate. Each module may only
519 * use this macro once, and calling it replaces module_init() and module_exit()
521 #define module_pnp_driver(__pnp_driver) \
522 module_driver(__pnp_driver, pnp_register_driver, \
523 pnp_unregister_driver)
525 #endif /* _LINUX_PNP_H */