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>
16 #define PNP_NAME_LEN 50
25 struct resource
*pnp_get_resource(struct pnp_dev
*dev
, unsigned long type
,
28 static inline struct resource
*pnp_get_resource(struct pnp_dev
*dev
,
29 unsigned long type
, unsigned int num
)
35 static inline int pnp_resource_valid(struct resource
*res
)
42 static inline int pnp_resource_enabled(struct resource
*res
)
44 if (res
&& !(res
->flags
& IORESOURCE_DISABLED
))
49 static inline resource_size_t
pnp_resource_len(struct resource
*res
)
51 if (res
->start
== 0 && res
->end
== 0)
53 return resource_size(res
);
57 static inline resource_size_t
pnp_port_start(struct pnp_dev
*dev
,
60 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IO
, bar
);
62 if (pnp_resource_valid(res
))
67 static inline resource_size_t
pnp_port_end(struct pnp_dev
*dev
,
70 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IO
, bar
);
72 if (pnp_resource_valid(res
))
77 static inline unsigned long pnp_port_flags(struct pnp_dev
*dev
,
80 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IO
, bar
);
82 if (pnp_resource_valid(res
))
84 return IORESOURCE_IO
| IORESOURCE_AUTO
;
87 static inline int pnp_port_valid(struct pnp_dev
*dev
, unsigned int bar
)
89 return pnp_resource_valid(pnp_get_resource(dev
, IORESOURCE_IO
, bar
));
92 static inline resource_size_t
pnp_port_len(struct pnp_dev
*dev
,
95 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IO
, bar
);
97 if (pnp_resource_valid(res
))
98 return pnp_resource_len(res
);
103 static inline resource_size_t
pnp_mem_start(struct pnp_dev
*dev
,
106 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_MEM
, bar
);
108 if (pnp_resource_valid(res
))
113 static inline resource_size_t
pnp_mem_end(struct pnp_dev
*dev
,
116 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_MEM
, bar
);
118 if (pnp_resource_valid(res
))
123 static inline unsigned long pnp_mem_flags(struct pnp_dev
*dev
, unsigned int bar
)
125 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_MEM
, bar
);
127 if (pnp_resource_valid(res
))
129 return IORESOURCE_MEM
| IORESOURCE_AUTO
;
132 static inline int pnp_mem_valid(struct pnp_dev
*dev
, unsigned int bar
)
134 return pnp_resource_valid(pnp_get_resource(dev
, IORESOURCE_MEM
, bar
));
137 static inline resource_size_t
pnp_mem_len(struct pnp_dev
*dev
,
140 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_MEM
, bar
);
142 if (pnp_resource_valid(res
))
143 return pnp_resource_len(res
);
148 static inline resource_size_t
pnp_irq(struct pnp_dev
*dev
, unsigned int bar
)
150 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IRQ
, bar
);
152 if (pnp_resource_valid(res
))
157 static inline unsigned long pnp_irq_flags(struct pnp_dev
*dev
, unsigned int bar
)
159 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IRQ
, bar
);
161 if (pnp_resource_valid(res
))
163 return IORESOURCE_IRQ
| IORESOURCE_AUTO
;
166 static inline int pnp_irq_valid(struct pnp_dev
*dev
, unsigned int bar
)
168 return pnp_resource_valid(pnp_get_resource(dev
, IORESOURCE_IRQ
, bar
));
172 static inline resource_size_t
pnp_dma(struct pnp_dev
*dev
, unsigned int bar
)
174 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_DMA
, bar
);
176 if (pnp_resource_valid(res
))
181 static inline unsigned long pnp_dma_flags(struct pnp_dev
*dev
, unsigned int bar
)
183 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_DMA
, bar
);
185 if (pnp_resource_valid(res
))
187 return IORESOURCE_DMA
| IORESOURCE_AUTO
;
190 static inline int pnp_dma_valid(struct pnp_dev
*dev
, unsigned int bar
)
192 return pnp_resource_valid(pnp_get_resource(dev
, IORESOURCE_DMA
, bar
));
201 struct device dev
; /* Driver Model device interface */
202 unsigned char number
; /* used as an index, must be unique */
203 struct list_head global_list
; /* node in global list of cards */
204 struct list_head protocol_list
; /* node in protocol's list of cards */
205 struct list_head devices
; /* devices attached to the card */
207 struct pnp_protocol
*protocol
;
208 struct pnp_id
*id
; /* contains supported EISA IDs */
210 char name
[PNP_NAME_LEN
]; /* contains a human-readable name */
211 unsigned char pnpver
; /* Plug & Play version */
212 unsigned char productver
; /* product version */
213 unsigned int serial
; /* serial number */
214 unsigned char checksum
; /* if zero - checksum passed */
215 struct proc_dir_entry
*procdir
; /* directory entry in /proc/bus/isapnp */
218 #define global_to_pnp_card(n) list_entry(n, struct pnp_card, global_list)
219 #define protocol_to_pnp_card(n) list_entry(n, struct pnp_card, protocol_list)
220 #define to_pnp_card(n) container_of(n, struct pnp_card, dev)
221 #define pnp_for_each_card(card) \
222 for((card) = global_to_pnp_card(pnp_cards.next); \
223 (card) != global_to_pnp_card(&pnp_cards); \
224 (card) = global_to_pnp_card((card)->global_list.next))
226 struct pnp_card_link
{
227 struct pnp_card
*card
;
228 struct pnp_card_driver
*driver
;
230 pm_message_t pm_state
;
233 static inline void *pnp_get_card_drvdata(struct pnp_card_link
*pcard
)
235 return pcard
->driver_data
;
238 static inline void pnp_set_card_drvdata(struct pnp_card_link
*pcard
, void *data
)
240 pcard
->driver_data
= data
;
244 struct device dev
; /* Driver Model device interface */
246 unsigned int number
; /* used as an index, must be unique */
249 struct list_head global_list
; /* node in global list of devices */
250 struct list_head protocol_list
; /* node in list of device's protocol */
251 struct list_head card_list
; /* node in card's list of devices */
252 struct list_head rdev_list
; /* node in cards list of requested devices */
254 struct pnp_protocol
*protocol
;
255 struct pnp_card
*card
; /* card the device is attached to, none if NULL */
256 struct pnp_driver
*driver
;
257 struct pnp_card_link
*card_link
;
259 struct pnp_id
*id
; /* supported EISA IDs */
263 unsigned int num_dependent_sets
;
264 struct list_head resources
;
265 struct list_head options
;
267 char name
[PNP_NAME_LEN
]; /* contains a human-readable name */
268 int flags
; /* used by protocols */
269 struct proc_dir_entry
*procent
; /* device entry in /proc/bus/isapnp */
273 #define global_to_pnp_dev(n) list_entry(n, struct pnp_dev, global_list)
274 #define card_to_pnp_dev(n) list_entry(n, struct pnp_dev, card_list)
275 #define protocol_to_pnp_dev(n) list_entry(n, struct pnp_dev, protocol_list)
276 #define to_pnp_dev(n) container_of(n, struct pnp_dev, dev)
277 #define pnp_for_each_dev(dev) \
278 for((dev) = global_to_pnp_dev(pnp_global.next); \
279 (dev) != global_to_pnp_dev(&pnp_global); \
280 (dev) = global_to_pnp_dev((dev)->global_list.next))
281 #define card_for_each_dev(card,dev) \
282 for((dev) = card_to_pnp_dev((card)->devices.next); \
283 (dev) != card_to_pnp_dev(&(card)->devices); \
284 (dev) = card_to_pnp_dev((dev)->card_list.next))
285 #define pnp_dev_name(dev) (dev)->name
287 static inline void *pnp_get_drvdata(struct pnp_dev
*pdev
)
289 return dev_get_drvdata(&pdev
->dev
);
292 static inline void pnp_set_drvdata(struct pnp_dev
*pdev
, void *data
)
294 dev_set_drvdata(&pdev
->dev
, data
);
299 void (*quirk_function
) (struct pnp_dev
* dev
); /* fixup function */
302 /* config parameters */
303 #define PNP_CONFIG_NORMAL 0x0001
304 #define PNP_CONFIG_FORCE 0x0002 /* disables validity checking */
307 #define PNP_READ 0x0001
308 #define PNP_WRITE 0x0002
309 #define PNP_DISABLE 0x0004
310 #define PNP_CONFIGURABLE 0x0008
311 #define PNP_REMOVABLE 0x0010
313 #define pnp_can_read(dev) (((dev)->protocol->get) && \
314 ((dev)->capabilities & PNP_READ))
315 #define pnp_can_write(dev) (((dev)->protocol->set) && \
316 ((dev)->capabilities & PNP_WRITE))
317 #define pnp_can_disable(dev) (((dev)->protocol->disable) && \
318 ((dev)->capabilities & PNP_DISABLE))
319 #define pnp_can_configure(dev) ((!(dev)->active) && \
320 ((dev)->capabilities & PNP_CONFIGURABLE))
323 extern struct pnp_protocol isapnp_protocol
;
324 #define pnp_device_is_isapnp(dev) ((dev)->protocol == (&isapnp_protocol))
326 #define pnp_device_is_isapnp(dev) 0
328 extern struct mutex pnp_res_mutex
;
330 #ifdef CONFIG_PNPBIOS
331 extern struct pnp_protocol pnpbios_protocol
;
332 #define pnp_device_is_pnpbios(dev) ((dev)->protocol == (&pnpbios_protocol))
334 #define pnp_device_is_pnpbios(dev) 0
337 #ifdef CONFIG_PNPACPI
338 extern struct pnp_protocol pnpacpi_protocol
;
340 static inline struct acpi_device
*pnp_acpi_device(struct pnp_dev
*dev
)
342 if (dev
->protocol
== &pnpacpi_protocol
)
347 #define pnp_acpi_device(dev) 0
351 #define PNP_READY 0x0000
352 #define PNP_ATTACHED 0x0001
353 #define PNP_BUSY 0x0002
354 #define PNP_FAULTY 0x0004
356 /* isapnp specific macros */
358 #define isapnp_card_number(dev) ((dev)->card ? (dev)->card->number : -1)
359 #define isapnp_csn_number(dev) ((dev)->number)
372 const struct pnp_device_id
*id_table
;
374 int (*probe
) (struct pnp_dev
*dev
, const struct pnp_device_id
*dev_id
);
375 void (*remove
) (struct pnp_dev
*dev
);
376 void (*shutdown
) (struct pnp_dev
*dev
);
377 int (*suspend
) (struct pnp_dev
*dev
, pm_message_t state
);
378 int (*resume
) (struct pnp_dev
*dev
);
379 struct device_driver driver
;
382 #define to_pnp_driver(drv) container_of(drv, struct pnp_driver, driver)
384 struct pnp_card_driver
{
385 struct list_head global_list
;
387 const struct pnp_card_device_id
*id_table
;
389 int (*probe
) (struct pnp_card_link
*card
,
390 const struct pnp_card_device_id
*card_id
);
391 void (*remove
) (struct pnp_card_link
*card
);
392 int (*suspend
) (struct pnp_card_link
*card
, pm_message_t state
);
393 int (*resume
) (struct pnp_card_link
*card
);
394 struct pnp_driver link
;
397 #define to_pnp_card_driver(drv) container_of(drv, struct pnp_card_driver, link)
399 /* pnp driver flags */
400 #define PNP_DRIVER_RES_DO_NOT_CHANGE 0x0001 /* do not change the state of the device */
401 #define PNP_DRIVER_RES_DISABLE 0x0003 /* ensure the device is disabled */
404 * Protocol Management
407 struct pnp_protocol
{
408 struct list_head protocol_list
;
411 /* resource control functions */
412 int (*get
) (struct pnp_dev
*dev
);
413 int (*set
) (struct pnp_dev
*dev
);
414 int (*disable
) (struct pnp_dev
*dev
);
416 /* protocol specific suspend/resume */
417 bool (*can_wakeup
) (struct pnp_dev
*dev
);
418 int (*suspend
) (struct pnp_dev
* dev
, pm_message_t state
);
419 int (*resume
) (struct pnp_dev
* dev
);
421 /* used by pnp layer only (look but don't touch) */
422 unsigned char number
; /* protocol number */
423 struct device dev
; /* link to driver model */
424 struct list_head cards
;
425 struct list_head devices
;
428 #define to_pnp_protocol(n) list_entry(n, struct pnp_protocol, protocol_list)
429 #define protocol_for_each_card(protocol,card) \
430 for((card) = protocol_to_pnp_card((protocol)->cards.next); \
431 (card) != protocol_to_pnp_card(&(protocol)->cards); \
432 (card) = protocol_to_pnp_card((card)->protocol_list.next))
433 #define protocol_for_each_dev(protocol,dev) \
434 for((dev) = protocol_to_pnp_dev((protocol)->devices.next); \
435 (dev) != protocol_to_pnp_dev(&(protocol)->devices); \
436 (dev) = protocol_to_pnp_dev((dev)->protocol_list.next))
438 extern struct bus_type pnp_bus_type
;
440 #if defined(CONFIG_PNP)
442 /* device management */
443 int pnp_device_attach(struct pnp_dev
*pnp_dev
);
444 void pnp_device_detach(struct pnp_dev
*pnp_dev
);
445 extern struct list_head pnp_global
;
446 extern int pnp_platform_devices
;
448 /* multidevice card support */
449 struct pnp_dev
*pnp_request_card_device(struct pnp_card_link
*clink
,
450 const char *id
, struct pnp_dev
*from
);
451 void pnp_release_card_device(struct pnp_dev
*dev
);
452 int pnp_register_card_driver(struct pnp_card_driver
*drv
);
453 void pnp_unregister_card_driver(struct pnp_card_driver
*drv
);
454 extern struct list_head pnp_cards
;
456 /* resource management */
457 int pnp_possible_config(struct pnp_dev
*dev
, int type
, resource_size_t base
,
458 resource_size_t size
);
459 int pnp_auto_config_dev(struct pnp_dev
*dev
);
460 int pnp_start_dev(struct pnp_dev
*dev
);
461 int pnp_stop_dev(struct pnp_dev
*dev
);
462 int pnp_activate_dev(struct pnp_dev
*dev
);
463 int pnp_disable_dev(struct pnp_dev
*dev
);
464 int pnp_range_reserved(resource_size_t start
, resource_size_t end
);
466 /* protocol helpers */
467 int pnp_is_active(struct pnp_dev
*dev
);
468 int compare_pnp_id(struct pnp_id
*pos
, const char *id
);
469 int pnp_register_driver(struct pnp_driver
*drv
);
470 void pnp_unregister_driver(struct pnp_driver
*drv
);
474 /* device management */
475 static inline int pnp_device_attach(struct pnp_dev
*pnp_dev
) { return -ENODEV
; }
476 static inline void pnp_device_detach(struct pnp_dev
*pnp_dev
) { }
478 #define pnp_platform_devices 0
480 /* multidevice card support */
481 static inline struct pnp_dev
*pnp_request_card_device(struct pnp_card_link
*clink
, const char *id
, struct pnp_dev
*from
) { return NULL
; }
482 static inline void pnp_release_card_device(struct pnp_dev
*dev
) { }
483 static inline int pnp_register_card_driver(struct pnp_card_driver
*drv
) { return -ENODEV
; }
484 static inline void pnp_unregister_card_driver(struct pnp_card_driver
*drv
) { }
486 /* resource management */
487 static inline int pnp_possible_config(struct pnp_dev
*dev
, int type
,
488 resource_size_t base
,
489 resource_size_t size
) { return 0; }
490 static inline int pnp_auto_config_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
491 static inline int pnp_start_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
492 static inline int pnp_stop_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
493 static inline int pnp_activate_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
494 static inline int pnp_disable_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
495 static inline int pnp_range_reserved(resource_size_t start
, resource_size_t end
) { return 0;}
497 /* protocol helpers */
498 static inline int pnp_is_active(struct pnp_dev
*dev
) { return 0; }
499 static inline int compare_pnp_id(struct pnp_id
*pos
, const char *id
) { return -ENODEV
; }
500 static inline int pnp_register_driver(struct pnp_driver
*drv
) { return -ENODEV
; }
501 static inline void pnp_unregister_driver(struct pnp_driver
*drv
) { }
503 #endif /* CONFIG_PNP */
505 #endif /* _LINUX_PNP_H */