2 * Linux Plug and Play Support
3 * Copyright by Adam Belay <ambx1@neo.rr.com>
9 #include <linux/device.h>
10 #include <linux/list.h>
11 #include <linux/errno.h>
12 #include <linux/mod_devicetable.h>
14 #define PNP_NAME_LEN 50
22 struct resource
*pnp_get_resource(struct pnp_dev
*, unsigned int, unsigned int);
24 static inline int pnp_resource_valid(struct resource
*res
)
31 static inline int pnp_resource_enabled(struct resource
*res
)
33 if (res
&& !(res
->flags
& IORESOURCE_DISABLED
))
38 static inline resource_size_t
pnp_resource_len(struct resource
*res
)
40 if (res
->start
== 0 && res
->end
== 0)
42 return res
->end
- res
->start
+ 1;
46 static inline resource_size_t
pnp_port_start(struct pnp_dev
*dev
,
49 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IO
, bar
);
51 if (pnp_resource_valid(res
))
56 static inline resource_size_t
pnp_port_end(struct pnp_dev
*dev
,
59 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IO
, bar
);
61 if (pnp_resource_valid(res
))
66 static inline unsigned long pnp_port_flags(struct pnp_dev
*dev
,
69 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IO
, bar
);
71 if (pnp_resource_valid(res
))
73 return IORESOURCE_IO
| IORESOURCE_AUTO
;
76 static inline int pnp_port_valid(struct pnp_dev
*dev
, unsigned int bar
)
78 return pnp_resource_valid(pnp_get_resource(dev
, IORESOURCE_IO
, bar
));
81 static inline resource_size_t
pnp_port_len(struct pnp_dev
*dev
,
84 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IO
, bar
);
86 if (pnp_resource_valid(res
))
87 return pnp_resource_len(res
);
92 static inline resource_size_t
pnp_mem_start(struct pnp_dev
*dev
,
95 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_MEM
, bar
);
97 if (pnp_resource_valid(res
))
102 static inline resource_size_t
pnp_mem_end(struct pnp_dev
*dev
,
105 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_MEM
, bar
);
107 if (pnp_resource_valid(res
))
112 static inline unsigned long pnp_mem_flags(struct pnp_dev
*dev
, unsigned int bar
)
114 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_MEM
, bar
);
116 if (pnp_resource_valid(res
))
118 return IORESOURCE_MEM
| IORESOURCE_AUTO
;
121 static inline int pnp_mem_valid(struct pnp_dev
*dev
, unsigned int bar
)
123 return pnp_resource_valid(pnp_get_resource(dev
, IORESOURCE_MEM
, bar
));
126 static inline resource_size_t
pnp_mem_len(struct pnp_dev
*dev
,
129 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_MEM
, bar
);
131 if (pnp_resource_valid(res
))
132 return pnp_resource_len(res
);
137 static inline resource_size_t
pnp_irq(struct pnp_dev
*dev
, unsigned int bar
)
139 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IRQ
, bar
);
141 if (pnp_resource_valid(res
))
146 static inline unsigned long pnp_irq_flags(struct pnp_dev
*dev
, unsigned int bar
)
148 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_IRQ
, bar
);
150 if (pnp_resource_valid(res
))
152 return IORESOURCE_IRQ
| IORESOURCE_AUTO
;
155 static inline int pnp_irq_valid(struct pnp_dev
*dev
, unsigned int bar
)
157 return pnp_resource_valid(pnp_get_resource(dev
, IORESOURCE_IRQ
, bar
));
161 static inline resource_size_t
pnp_dma(struct pnp_dev
*dev
, unsigned int bar
)
163 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_DMA
, bar
);
165 if (pnp_resource_valid(res
))
170 static inline unsigned long pnp_dma_flags(struct pnp_dev
*dev
, unsigned int bar
)
172 struct resource
*res
= pnp_get_resource(dev
, IORESOURCE_DMA
, bar
);
174 if (pnp_resource_valid(res
))
176 return IORESOURCE_DMA
| IORESOURCE_AUTO
;
179 static inline int pnp_dma_valid(struct pnp_dev
*dev
, unsigned int bar
)
181 return pnp_resource_valid(pnp_get_resource(dev
, IORESOURCE_DMA
, bar
));
186 unsigned short min
; /* min base number */
187 unsigned short max
; /* max base number */
188 unsigned char align
; /* align boundary */
189 unsigned char size
; /* size of range */
190 unsigned char flags
; /* port flags */
191 unsigned char pad
; /* pad */
192 struct pnp_port
*next
; /* next port */
195 #define PNP_IRQ_NR 256
197 DECLARE_BITMAP(map
, PNP_IRQ_NR
); /* bitmask for IRQ lines */
198 unsigned char flags
; /* IRQ flags */
199 unsigned char pad
; /* pad */
200 struct pnp_irq
*next
; /* next IRQ */
204 unsigned char map
; /* bitmask for DMA channels */
205 unsigned char flags
; /* DMA flags */
206 struct pnp_dma
*next
; /* next port */
210 unsigned int min
; /* min base number */
211 unsigned int max
; /* max base number */
212 unsigned int align
; /* align boundary */
213 unsigned int size
; /* size of range */
214 unsigned char flags
; /* memory flags */
215 unsigned char pad
; /* pad */
216 struct pnp_mem
*next
; /* next memory resource */
219 #define PNP_RES_PRIORITY_PREFERRED 0
220 #define PNP_RES_PRIORITY_ACCEPTABLE 1
221 #define PNP_RES_PRIORITY_FUNCTIONAL 2
222 #define PNP_RES_PRIORITY_INVALID 65535
225 unsigned short priority
; /* priority */
226 struct pnp_port
*port
; /* first port */
227 struct pnp_irq
*irq
; /* first IRQ */
228 struct pnp_dma
*dma
; /* first DMA */
229 struct pnp_mem
*mem
; /* first memory resource */
230 struct pnp_option
*next
; /* used to chain dependent resources */
238 struct device dev
; /* Driver Model device interface */
239 unsigned char number
; /* used as an index, must be unique */
240 struct list_head global_list
; /* node in global list of cards */
241 struct list_head protocol_list
; /* node in protocol's list of cards */
242 struct list_head devices
; /* devices attached to the card */
244 struct pnp_protocol
*protocol
;
245 struct pnp_id
*id
; /* contains supported EISA IDs */
247 char name
[PNP_NAME_LEN
]; /* contains a human-readable name */
248 unsigned char pnpver
; /* Plug & Play version */
249 unsigned char productver
; /* product version */
250 unsigned int serial
; /* serial number */
251 unsigned char checksum
; /* if zero - checksum passed */
252 struct proc_dir_entry
*procdir
; /* directory entry in /proc/bus/isapnp */
255 #define global_to_pnp_card(n) list_entry(n, struct pnp_card, global_list)
256 #define protocol_to_pnp_card(n) list_entry(n, struct pnp_card, protocol_list)
257 #define to_pnp_card(n) container_of(n, struct pnp_card, dev)
258 #define pnp_for_each_card(card) \
259 for((card) = global_to_pnp_card(pnp_cards.next); \
260 (card) != global_to_pnp_card(&pnp_cards); \
261 (card) = global_to_pnp_card((card)->global_list.next))
263 struct pnp_card_link
{
264 struct pnp_card
*card
;
265 struct pnp_card_driver
*driver
;
267 pm_message_t pm_state
;
270 static inline void *pnp_get_card_drvdata(struct pnp_card_link
*pcard
)
272 return pcard
->driver_data
;
275 static inline void pnp_set_card_drvdata(struct pnp_card_link
*pcard
, void *data
)
277 pcard
->driver_data
= data
;
281 struct device dev
; /* Driver Model device interface */
283 unsigned int number
; /* used as an index, must be unique */
286 struct list_head global_list
; /* node in global list of devices */
287 struct list_head protocol_list
; /* node in list of device's protocol */
288 struct list_head card_list
; /* node in card's list of devices */
289 struct list_head rdev_list
; /* node in cards list of requested devices */
291 struct pnp_protocol
*protocol
;
292 struct pnp_card
*card
; /* card the device is attached to, none if NULL */
293 struct pnp_driver
*driver
;
294 struct pnp_card_link
*card_link
;
296 struct pnp_id
*id
; /* supported EISA IDs */
300 struct pnp_option
*independent
;
301 struct pnp_option
*dependent
;
302 struct list_head resources
;
304 char name
[PNP_NAME_LEN
]; /* contains a human-readable name */
305 int flags
; /* used by protocols */
306 struct proc_dir_entry
*procent
; /* device entry in /proc/bus/isapnp */
310 #define global_to_pnp_dev(n) list_entry(n, struct pnp_dev, global_list)
311 #define card_to_pnp_dev(n) list_entry(n, struct pnp_dev, card_list)
312 #define protocol_to_pnp_dev(n) list_entry(n, struct pnp_dev, protocol_list)
313 #define to_pnp_dev(n) container_of(n, struct pnp_dev, dev)
314 #define pnp_for_each_dev(dev) \
315 for((dev) = global_to_pnp_dev(pnp_global.next); \
316 (dev) != global_to_pnp_dev(&pnp_global); \
317 (dev) = global_to_pnp_dev((dev)->global_list.next))
318 #define card_for_each_dev(card,dev) \
319 for((dev) = card_to_pnp_dev((card)->devices.next); \
320 (dev) != card_to_pnp_dev(&(card)->devices); \
321 (dev) = card_to_pnp_dev((dev)->card_list.next))
322 #define pnp_dev_name(dev) (dev)->name
324 static inline void *pnp_get_drvdata(struct pnp_dev
*pdev
)
326 return dev_get_drvdata(&pdev
->dev
);
329 static inline void pnp_set_drvdata(struct pnp_dev
*pdev
, void *data
)
331 dev_set_drvdata(&pdev
->dev
, data
);
336 void (*quirk_function
) (struct pnp_dev
* dev
); /* fixup function */
339 /* config parameters */
340 #define PNP_CONFIG_NORMAL 0x0001
341 #define PNP_CONFIG_FORCE 0x0002 /* disables validity checking */
344 #define PNP_READ 0x0001
345 #define PNP_WRITE 0x0002
346 #define PNP_DISABLE 0x0004
347 #define PNP_CONFIGURABLE 0x0008
348 #define PNP_REMOVABLE 0x0010
350 #define pnp_can_read(dev) (((dev)->protocol->get) && \
351 ((dev)->capabilities & PNP_READ))
352 #define pnp_can_write(dev) (((dev)->protocol->set) && \
353 ((dev)->capabilities & PNP_WRITE))
354 #define pnp_can_disable(dev) (((dev)->protocol->disable) && \
355 ((dev)->capabilities & PNP_DISABLE))
356 #define pnp_can_configure(dev) ((!(dev)->active) && \
357 ((dev)->capabilities & PNP_CONFIGURABLE))
360 extern struct pnp_protocol isapnp_protocol
;
361 #define pnp_device_is_isapnp(dev) ((dev)->protocol == (&isapnp_protocol))
363 #define pnp_device_is_isapnp(dev) 0
365 extern struct mutex pnp_res_mutex
;
367 #ifdef CONFIG_PNPBIOS
368 extern struct pnp_protocol pnpbios_protocol
;
369 #define pnp_device_is_pnpbios(dev) ((dev)->protocol == (&pnpbios_protocol))
371 #define pnp_device_is_pnpbios(dev) 0
375 #define PNP_READY 0x0000
376 #define PNP_ATTACHED 0x0001
377 #define PNP_BUSY 0x0002
378 #define PNP_FAULTY 0x0004
380 /* isapnp specific macros */
382 #define isapnp_card_number(dev) ((dev)->card ? (dev)->card->number : -1)
383 #define isapnp_csn_number(dev) ((dev)->number)
396 const struct pnp_device_id
*id_table
;
398 int (*probe
) (struct pnp_dev
*dev
, const struct pnp_device_id
*dev_id
);
399 void (*remove
) (struct pnp_dev
*dev
);
400 int (*suspend
) (struct pnp_dev
*dev
, pm_message_t state
);
401 int (*resume
) (struct pnp_dev
*dev
);
402 struct device_driver driver
;
405 #define to_pnp_driver(drv) container_of(drv, struct pnp_driver, driver)
407 struct pnp_card_driver
{
408 struct list_head global_list
;
410 const struct pnp_card_device_id
*id_table
;
412 int (*probe
) (struct pnp_card_link
*card
,
413 const struct pnp_card_device_id
*card_id
);
414 void (*remove
) (struct pnp_card_link
*card
);
415 int (*suspend
) (struct pnp_card_link
*card
, pm_message_t state
);
416 int (*resume
) (struct pnp_card_link
*card
);
417 struct pnp_driver link
;
420 #define to_pnp_card_driver(drv) container_of(drv, struct pnp_card_driver, link)
422 /* pnp driver flags */
423 #define PNP_DRIVER_RES_DO_NOT_CHANGE 0x0001 /* do not change the state of the device */
424 #define PNP_DRIVER_RES_DISABLE 0x0003 /* ensure the device is disabled */
427 * Protocol Management
430 struct pnp_protocol
{
431 struct list_head protocol_list
;
434 /* resource control functions */
435 int (*get
) (struct pnp_dev
*dev
);
436 int (*set
) (struct pnp_dev
*dev
);
437 int (*disable
) (struct pnp_dev
*dev
);
439 /* protocol specific suspend/resume */
440 int (*suspend
) (struct pnp_dev
* dev
, pm_message_t state
);
441 int (*resume
) (struct pnp_dev
* dev
);
443 /* used by pnp layer only (look but don't touch) */
444 unsigned char number
; /* protocol number */
445 struct device dev
; /* link to driver model */
446 struct list_head cards
;
447 struct list_head devices
;
450 #define to_pnp_protocol(n) list_entry(n, struct pnp_protocol, protocol_list)
451 #define protocol_for_each_card(protocol,card) \
452 for((card) = protocol_to_pnp_card((protocol)->cards.next); \
453 (card) != protocol_to_pnp_card(&(protocol)->cards); \
454 (card) = protocol_to_pnp_card((card)->protocol_list.next))
455 #define protocol_for_each_dev(protocol,dev) \
456 for((dev) = protocol_to_pnp_dev((protocol)->devices.next); \
457 (dev) != protocol_to_pnp_dev(&(protocol)->devices); \
458 (dev) = protocol_to_pnp_dev((dev)->protocol_list.next))
460 extern struct bus_type pnp_bus_type
;
462 #if defined(CONFIG_PNP)
464 /* device management */
465 int pnp_device_attach(struct pnp_dev
*pnp_dev
);
466 void pnp_device_detach(struct pnp_dev
*pnp_dev
);
467 extern struct list_head pnp_global
;
468 extern int pnp_platform_devices
;
470 /* multidevice card support */
471 struct pnp_dev
*pnp_request_card_device(struct pnp_card_link
*clink
,
472 const char *id
, struct pnp_dev
*from
);
473 void pnp_release_card_device(struct pnp_dev
*dev
);
474 int pnp_register_card_driver(struct pnp_card_driver
*drv
);
475 void pnp_unregister_card_driver(struct pnp_card_driver
*drv
);
476 extern struct list_head pnp_cards
;
478 /* resource management */
479 int pnp_possible_config(struct pnp_dev
*dev
, int type
, resource_size_t base
,
480 resource_size_t size
);
481 int pnp_auto_config_dev(struct pnp_dev
*dev
);
482 int pnp_start_dev(struct pnp_dev
*dev
);
483 int pnp_stop_dev(struct pnp_dev
*dev
);
484 int pnp_activate_dev(struct pnp_dev
*dev
);
485 int pnp_disable_dev(struct pnp_dev
*dev
);
487 /* protocol helpers */
488 int pnp_is_active(struct pnp_dev
*dev
);
489 int compare_pnp_id(struct pnp_id
*pos
, const char *id
);
490 int pnp_register_driver(struct pnp_driver
*drv
);
491 void pnp_unregister_driver(struct pnp_driver
*drv
);
495 /* device management */
496 static inline int pnp_device_attach(struct pnp_dev
*pnp_dev
) { return -ENODEV
; }
497 static inline void pnp_device_detach(struct pnp_dev
*pnp_dev
) { }
499 #define pnp_platform_devices 0
501 /* multidevice card support */
502 static inline struct pnp_dev
*pnp_request_card_device(struct pnp_card_link
*clink
, const char *id
, struct pnp_dev
*from
) { return NULL
; }
503 static inline void pnp_release_card_device(struct pnp_dev
*dev
) { }
504 static inline int pnp_register_card_driver(struct pnp_card_driver
*drv
) { return -ENODEV
; }
505 static inline void pnp_unregister_card_driver(struct pnp_card_driver
*drv
) { }
507 /* resource management */
508 static inline int pnp_possible_config(struct pnp_dev
*dev
, int type
,
509 resource_size_t base
,
510 resource_size_t size
) { return 0; }
511 static inline int pnp_auto_config_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
512 static inline int pnp_start_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
513 static inline int pnp_stop_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
514 static inline int pnp_activate_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
515 static inline int pnp_disable_dev(struct pnp_dev
*dev
) { return -ENODEV
; }
517 /* protocol helpers */
518 static inline int pnp_is_active(struct pnp_dev
*dev
) { return 0; }
519 static inline int compare_pnp_id(struct pnp_id
*pos
, const char *id
) { return -ENODEV
; }
520 static inline int pnp_register_driver(struct pnp_driver
*drv
) { return -ENODEV
; }
521 static inline void pnp_unregister_driver(struct pnp_driver
*drv
) { }
523 #endif /* CONFIG_PNP */
525 #define pnp_err(format, arg...) printk(KERN_ERR "pnp: " format "\n" , ## arg)
526 #define pnp_info(format, arg...) printk(KERN_INFO "pnp: " format "\n" , ## arg)
527 #define pnp_warn(format, arg...) printk(KERN_WARNING "pnp: " format "\n" , ## arg)
529 #ifdef CONFIG_PNP_DEBUG
530 #define pnp_dbg(format, arg...) printk(KERN_DEBUG "pnp: " format "\n" , ## arg)
532 #define pnp_dbg(format, arg...) do {} while (0)
535 #endif /* _LINUX_PNP_H */